-
Notifications
You must be signed in to change notification settings - Fork 1
GooByExample
Bruce Mitchener edited this page Jan 6, 2014
·
1 revision
See GTLoop for looping, GTProgramControl for program control structure examples, and GTSimplePrograms for simple miniature programs.
Simple Function
(df double (x) (* 2 x))
Output a message
(df hello-world () (post "Hello World!\n"))
Output a slightly more complicated message
(df hello-number (n) (post "Hello number %=!\n" n))
Attach types to arguments
(df hello-number (n|<int>) (post "Hello integer %=!\n" n))
Attach a type to your return (type)
(df double (x|<int> => <int>) (* 2 x))
Use generics for MultiMethodDispatching
(dm hello (x|<int>) (post "Hello integer %=!\n" x)) (dm hello (x|<flo>) (post "Hello floating point %=!\n" x))
Fun with string concatenation
(dm did-you? (thing|<str>) (cat "No! I didn't " thing)) (did-you? "eat all the cookies in the cookie jar") "No! I didn't eat all the cookies in the cookie jar"
Use def to define variables in your functions (generics too)
(dm formal-hello (name|<str>) (def formal-name (cat "mister " name)) (post "Hello %=\n" formal-name))
Define functions / closures as needed
(df quadruple (x|<int>) (def double (fun (y) (* 2 y))) (double (double x)))
Collection processing for fun and profit
(dm make-names-stupid (names|<col>) (map (fun (the-name) (cat "Smelly " the-name)) names)) (dm insult-many-people () (def people (tup "Santa" "Easter Bunny" "Ringo")) (do (fun (name) (post "%s is Smelly!\n" name)) (make-names-stupid people))) (insult-many-people) Smelly Santa is Smelly! Smelly Easter Bunny is Smelly! Smelly Ringo is Smelly!
Classes
(dc <cat> (<any>)) (dp bathroom (<cat> => <any>) "dirt") (dc <dog> (<any>)) (dp bathroom (<dog> => <any>) "tree") (set my-cat (new <cat>)) (set my-dog (new <dog>)) (bathroom my-cat) => "dirt" (bathroom my-dog) => "tree"