Last active
January 22, 2016 08:52
-
-
Save owickstrom/a02c9674cc455f52da2b to your computer and use it in GitHub Desktop.
Alternate syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(pkg main) | |
(import fmt) | |
;; if expressions | |
(: fib (int -> int)) | |
(def (fib n) | |
(if (== n 1) | |
0 | |
(if (== n 2) | |
1 | |
(+ (fib (- n 1)) (fib (- n 2)))))) | |
;; match expression | |
(: fib2 (int -> int)) | |
(def (fib2 n) | |
(match n | |
(1 0) | |
(2 1) | |
(_ (+ (fib (- n 1)) (fib (- n 2)))))) | |
;; match expression | |
(: fib3 (int -> int)) | |
(def | |
(fib3 1) 0 | |
(fib3 2) 1 | |
(fib3 n) (+ (fib (- n 1)) (fib (- n 2)))) | |
;; type variables | |
(: identity (#a -> #a)) | |
(def (identity x) x) | |
;; explicitly quantified | |
(: twice (forall (#a) (#a -> #a))) | |
(def (twice f x) (f (f x))) | |
;; no-arg fn | |
(: main (-> unit)) | |
(def (main) (fmt.Println (fib 10))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pkg main | |
import fmt | |
// if expressions | |
fib(n int) int -> | |
if n == 1 { | |
0 | |
} else if n == 2 { | |
1 | |
} else { | |
fib (n - 1) + fib (n - 2) | |
} | |
// match expression | |
fib2(n int) int -> match n { | |
1 -> 0 | |
2 -> 1 | |
_ -> fib(n - 1) + fib(n - 2) | |
} | |
// argument pattern matching, how to do it now?! | |
// type variables | |
identity(x #a) #a -> x | |
// explicitly quantified | |
twice<#a>(f (#a -> #a), x #a) #a -> f (f x) | |
// no-arg fn | |
main() unit -> fmt.Println (fib 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pkg main | |
import fmt | |
// if expressions | |
fib :: int -> int | |
fib(n) -> | |
if n == 1 { | |
0 | |
} else if n == 2 { | |
1 | |
} else { | |
fib (n - 1) + fib (n - 2) | |
} | |
// match expression | |
fib2 :: int -> int | |
fib2(n) -> match n { | |
1 -> 0 | |
2 -> 1 | |
_ -> fib(n - 1) + fib(n - 2) | |
} | |
// argument pattern matching | |
fib3 :: int -> int | |
fib3(1) -> 0 | |
fib3(2) -> 1 | |
fib3(n) -> fib3(n - 1) + fib3(n - 2) | |
// type variables | |
identity :: #a -> #a | |
identity(x) -> x | |
// explicitly quantified | |
twice :: forall #a. (#a -> #a) -> #a -> #a | |
twice(f, x) -> f (f x) | |
// no-arg fn | |
main :: -> unit | |
main() -> fmt.Println (fib 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment