Created
February 7, 2014 16:40
-
-
Save rikusalminen/8866479 to your computer and use it in GitHub Desktop.
Ideas for a toy programming language project
This file contains hidden or 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
| fib(n) = | |
| if n <= 1 | |
| then n // TODO: is "then" keyword required? not strictly but improves readability? | |
| else | |
| left = fib(n-2) | |
| right = fib(n-1) | |
| left + right | |
| fib_iter(n) = | |
| iter(a, b, n) = | |
| if n == 0 then a | |
| elif n == 1 then b | |
| else iter(b, a+b, n-1) | |
| iter(0, 1, n) | |
| example_declarations(x) = | |
| // NOTE: declarations are like letrec expressions in Scheme/ML/Haskell | |
| // declarations are *not* like assignments in C or Python | |
| f(w) = w * (x + y) * z | |
| y = z + x | |
| z = 42 | |
| f(x) | |
| mutual_recursion(x) = | |
| even(x) = if x == 0 then true else odd(x-1) | |
| odd(x) = if x == 0 then false else even(x-1) | |
| even(x) != odd(x) | |
| mutual_recursion2(x) = | |
| // short circuiting (?) | |
| even(x) = x == 0 || odd(x-1) | |
| odd(x) = x == 1 || even(x-1) | |
| even(x) != odd(x) | |
| semicolons_are_for_seqencing() = | |
| print("hello "); print("world\n") // NOTE: no semicolon allowed in the end! | |
| semicolons_are_for_seqencing2() = | |
| // prints: why y\n 43 55 | |
| x = 1 | |
| y = print("why y\n"); 42 | |
| print(x + y); | |
| x = 13 // NOTE: this is *not* an re-assignment | |
| print(x + y) | |
| semicolons_are_for_seqencing3() = | |
| // prints: "x\ny\n" OR "y\nx\n" (no semicolon, no ordering) | |
| x = print("x\n"); 42 | |
| y = print("y\n"); 13 | |
| x + y | |
| semicolons_are_for_seqencing4() = | |
| // prints: "x\ny\n" IN THAT ORDER | |
| x = print("x\n"); 42 | |
| pass; // add a "dummy" sequencing point | |
| y = print("y\n"); 13 | |
| x + y | |
| semicolons_are_for_seqencing5() = | |
| // this is a contrived example, don't write code like this! | |
| // prints: 42 13 | |
| x = 42 | |
| y = print(x); x = 13 print(x); x+1 | |
| x+y // returns: (42+14) = 56 | |
| higher_order(cmp, a b) = | |
| // type: forall a. ((a, a) -> bool, a, a) -> a | |
| if cmp(a, b) then a else b | |
| // here's some legal but awkward examples | |
| fucked_up1() = | |
| // TODO: should there be parenthesis required around if-then-else | |
| f(if a then b else c, d) | |
| fucked_up2() = | |
| // let declarations inside a function call are awkward too (add parens?) | |
| f(x = 42 y = 13 x+y, z) | |
| fucked_up3() = | |
| // confused yet? | |
| f(print("hello"); print("world"); x+y, z) | |
| fucked_up4() = | |
| // can even parenthesis save this one? | |
| f(if foo(); bar then quux else x = 42 y = 13 print(x); print(y); x*y, z) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment