├── Functions.md └── README.md /Functions.md: -------------------------------------------------------------------------------- 1 | # Functions 2 | 3 | ```js 4 | // unary 5 | a:boolean.not -> boolean = 6 | a.if_true false if_false true 7 | 8 | // binary and above 9 | a:boolean.and b:boolean -> boolean = 10 | a.if_true b if_false false 11 | 12 | a:string.replace old:string with new:string -> string = 13 | a.split_by old.join_by new 14 | 15 | // precedence 16 | // just from left to right, nothing special 17 | true.ifTrue(true)ifFalse(true.and(false.not)) 18 | ``` 19 | 20 | # Record type 21 | 22 | ```js 23 | // definition 24 | people = data. 25 | kind #people 26 | name string 27 | age integer 28 | friend (people.maybe) 29 | 30 | // construction (longer version) 31 | x = people.name "Hello" age 9 friend nil 32 | 33 | // construction (shorter version) 34 | x = people.new("hello", 9, nil) 35 | 36 | // access 37 | console.log(x.name.to_upper) 38 | 39 | // update 40 | y = x.name "lee".age 9 41 | console.log(y) // {name: "wong", } 42 | ``` 43 | 44 | ## Binary Tree example 45 | 46 | ```php 47 | 48 | // defining an interface 49 | comparable = interface.new 50 | 51 | // defining what functions need to be defined in an interface 52 | 53 | x:a .> y:a -> boolean = undefined 54 | 55 | 56 | x:a .== y:a -> boolean = undefined 57 | 58 | // defining default implementations for some functions 59 | 60 | x:a .!= y:a -> boolean = x.== y.not 61 | 62 | color = data. 63 | kind #color 64 | r integer 65 | g integer 66 | b integer 67 | 68 | // stating that a data should implement an interface 69 | color.implements comparable 70 | 71 | // implementing 72 | x:color .== y:color -> boolean = x.r .== (y.r) 73 | 74 | 75 | 76 | a:comparable.btree -> type = 77 | (data. 78 | kind #leaf) 79 | .or 80 | (data. 81 | kind #node 82 | left (a.btree) 83 | current a 84 | right (a.btree)) 85 | 86 | // The following things will be generated automaticall 87 | // 0) Constructors 88 | // 0.1) Long constructor 89 | // 0.2) Short constructor 90 | // 1) Getters 91 | // 2) Setters 92 | // 3) Switchers (e.g. if_xxx if_yyy) 93 | 94 | 95 | (x:a.btree).insert value:a -> a.btree = 96 | x. 97 | if_leaf (node.new(leaf, value, leaf)) 98 | if_node 99 | (value .< (x.value). 100 | if_true (x.left (x.left.insert value)) 101 | if_false (x.right (x.right.insert value))) 102 | 103 | 104 | (x:a.btree).to_list -> a.list = 105 | x. 106 | if_nil (a.list.new) 107 | if_cons (x.left.to_list.append(x.current).concat(x.right.toList)) 108 | 109 | 110 | (x:a.list).to_btree -> a.btree = 111 | x. 112 | if_nil leaf 113 | if_cons (node.new(leaf, x, leaf).insert(x.next)) 114 | ``` 115 | 116 | ## Lambdas 117 | 118 | ```php 119 | 120 | xs:(a.list) .map f:(a.to b) -> b.list = 121 | xs. 122 | if_nil [] 123 | if_cons [f.apply(xs.current)].concat(xs.next.map(f))) 124 | 125 | y = [1,2,3].map($ + 2) 126 | z = [1,2,3].map((x) -> x + 2) 127 | 128 | // single parameter lambda 129 | f = $ + 2 // or 130 | f = ((x) -> x + 2) 131 | 132 | // double parameter lambda 133 | f = $ + $$ // or 134 | f = ((x, y) -> x + y) 135 | ``` 136 | 137 | ## Monads 138 | 139 | ```hs 140 | class Monad m where 141 | (>>=) :: m a -> ( a -> m b) -> m b 142 | (>>) :: m a -> m b -> m b 143 | return :: a -> m a 144 | fail :: String -> m a 145 | 146 | monad = interface.new 147 | 148 | {m:monad} 149 | x:(a.m) .bind f:(a) 150 | 151 | ``` -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Keli language 2 | 3 | ## Motivation 4 | 5 | The following is an excerpt from [Quora](https://www.quora.com/Why-isnt-functional-programming-that-popular-even-though-its-so-beneficial): 6 | 7 | > The user experiences of functional programming languages sucks. 8 | 9 | Keli language aims to create a FP language that has good user experience. 10 | 11 | But, what kind of languages have nice UX? I would say Smalltalk, it is the languages that revolutionarize how we code with the following features: 12 | 13 | - Code completion 14 | - Intellisense 15 | 16 | But sadly, most FP languages (for example Haskell) has very poor IDE support, firstly I think this is due to the syntax of Haskell itself. Consider language like Smalltalk and Java, it is easy to implement Intellisense for them due to the **dot notation**. 17 | 18 | Thus, Keli language can be seen as an attempt to combine Haskell with Smalltalk. 19 | --------------------------------------------------------------------------------