September 9 lecture notes - intro programming w/ mathematica ------------------------------------------------------------- I) questions 1) Mathematica notebooks 2) entry of symbols (Traditional Form) 3) functions lists (Standard Form) II) a bit more logic 1) x<10 && y>5 x<10 || y>10 true or false? 2) if (a) then (b) Implies[a,b] a => b 3) For All, Exists 4) See silly example of logic puzzle (from Rev. Dodgeson) in code/ 5) Class exercise: a) do truth table for a=>b b) find a way to write a=>b using !,&&,|| III) more on lists a = {1,2,3,4} first element is a[[1]] or is it... check out a[[0]] and FullForm[a] For a really good time, try FullForm[a/b], (a/b)[[0]] Also see Part[] *** everything in mathematica is really a list *** Also see Hold[] (stops something from being evaluated) Also see NumberMarks, 1.2` for approximate results IV) functions in mathematica f[x]=...; has TWO PROBLEMS : 1st, this matches only "x" exactly. Mathematica allows multiple definitions; it uses the most specific. example: f[1]=1; f[0]=1; f[x]=f[x-1]+f[x-2]; 2nd, this evaluates the RHS right away, and stores result. example: f[x]=StringJoin["**",ToString[x],"**"]; To fix both problems, use f[x_]:= (put definition here); 1) x_ means "match any single thing" 2) := means "delay evaluation until later" example: f[]:= Print["Hi there!\n"]; (* This line defines a subroutine/program, *) f[] enter (* and this runs it *) Note that comments in Mathematica go within (* *) marks. V) "Hello world" subroutine in other languages --- C ------------------------- one of the classics void f(){ printf("Hi there!\n"); } --- perl ---------------------- often Jim's choice sub f { print "Hi there!\n"; } --- Java ---------------------- popular in typical college classes class Hello{ public static void main(String[] args){ System.out.println("Hi there!\n"); } } --- javascript ------------------ what you'll see in a web page --- lisp ---------------------- popular for AI research (defun hi () (format t "Hi there!"))