|
| 1 | + |
| 2 | +{- # Bindings -} |
| 3 | + |
| 4 | + |
| 5 | +-- Value definition |
| 6 | +def a = 1 |
| 7 | + |
| 8 | + |
| 9 | +-- Value definition with type annotation |
| 10 | +def a :: Int = 1 |
| 11 | + |
| 12 | + |
| 13 | +-- Multiple recursive value definitions |
| 14 | +def a = 1, |
| 15 | + b = c + a, |
| 16 | + c = a + 10 |
| 17 | + |
| 18 | + |
| 19 | +-- Function definition |
| 20 | +def f a = |
| 21 | + a + 1 |
| 22 | + |
| 23 | + |
| 24 | +-- Function definition with block |
| 25 | +def f a = |
| 26 | + let result = a + 1 in |
| 27 | + print "result = $result" |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +-- Function definition with type annotation |
| 32 | +def f a :: Int -> Int = |
| 33 | + a + 1 |
| 34 | + |
| 35 | + |
| 36 | +-- Function definition with inline type annotation |
| 37 | +def f (a :: Int) -> Int = |
| 38 | + a + 1 |
| 39 | + |
| 40 | + |
| 41 | +-- Function definitions with type declaration |
| 42 | +def f :: Int -> Int |
| 43 | +def f a = a + 1 |
| 44 | + |
| 45 | + |
| 46 | +-- Function definitions with pattern matching |
| 47 | +def f 1 = 0 |
| 48 | + | f a = a + 1 |
| 49 | + |
| 50 | + |
| 51 | +-- Function definition as a value |
| 52 | +def f = a -> a + 1 |
| 53 | + |
| 54 | + |
| 55 | +-- [calculate_magic] prints a string representation of a magic number. |
| 56 | +def calculate_magic () = |
| 57 | + let |
| 58 | + magic_number = 42 + fib another_regular_number, |
| 59 | + regular_number = 1, |
| 60 | + another_regular_number = 2 |
| 61 | + |
| 62 | + let add_one x = x + 1 |
| 63 | + |
| 64 | + let factorial n = |
| 65 | + match n |
| 66 | + | 0 -> 1 |
| 67 | + | n -> n * factorial (n - 1) |
| 68 | + end |
| 69 | + |
| 70 | + let fib n = |
| 71 | + if n == 0 || n == 1 then 1 |
| 72 | + else fib (n - 1) + fib (n - 2) |
| 73 | + |
| 74 | + in |
| 75 | + print ("The value of fib (magic_number + regular_number) is " |
| 76 | + ++ string (fib (magic_number + regular_number)) ++ "!") |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +-- * -- |
| 86 | + |
| 87 | + |
| 88 | +-- ### Semicolons for Definitions |
| 89 | + |
| 90 | +let x = f x y z; e |
| 91 | + => let x = f x y z in e |
| 92 | + |
| 93 | +f x y z; e |
| 94 | + => let () = f x y z in e |
| 95 | + |
| 96 | + |
| 97 | +{- [calculate_magic] prints a string representation of a magic number. -} |
| 98 | +fun calculate_magic () = |
| 99 | + val magic_number = 42 + fib another_regular_number, |
| 100 | + regular_number = 1, |
| 101 | + another_regular_number = 2, |
| 102 | + f x = 2 |
| 103 | + |
| 104 | + fun add_one x = x + 1 |
| 105 | + |
| 106 | + fun factorial n = |
| 107 | + match n |
| 108 | + | 0 => 1 |
| 109 | + | n => n * factorial (n - 1) |
| 110 | + end |
| 111 | + |
| 112 | + fun fib 0 = 1 |
| 113 | + | fib 1 = 1 |
| 114 | + | fib n = fib (n - 1) + fib (n - 2) |
| 115 | + |
| 116 | + in |
| 117 | + print ("The value of fib (magic_number + regular_number) is " |
| 118 | + ++ string (fib (magic_number + regular_number)) ++ "!") |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +-- ## Let Expressions |
| 123 | + |
| 124 | +let a = 1 |
| 125 | +print "The value of a is $a!" |
| 126 | + |
| 127 | +let f a = a + 1 |
| 128 | +print "The value of (f a) is $(f 4)!" |
| 129 | + |
| 130 | +let test () => |
| 131 | + let |
| 132 | + a = 1 |
| 133 | + |
| 134 | + f b = b + 1 |
| 135 | + |
| 136 | + factorial => |
| 137 | + match n |
| 138 | + | 0 -> 1 |
| 139 | + | n -> n * factorial (n - 1) |
| 140 | + end |
| 141 | + |
| 142 | + factorial2 = |
| 143 | + | 0 -> 1 |
| 144 | + | n -> n * factorial (n - 1), |
| 145 | + |
| 146 | + c = 3 |
| 147 | + in |
| 148 | + print "The value of a, f b, c are $a, $(f 4), $c!" |
| 149 | + |
| 150 | + |
| 151 | +-- ## Where Expressions |
| 152 | + |
| 153 | +print "The value of a is $a!" |
| 154 | +where a = 1 |
| 155 | + |
| 156 | +print "The value of (f a) is $(f 4)!" |
| 157 | +where f a = a + 1 |
| 158 | + |
| 159 | +print "The value of a, f b, c are $a, $(f 4), $c!" |
| 160 | +where |
| 161 | + a = 1 |
| 162 | + |
| 163 | + f b = b + 1 |
| 164 | + |
| 165 | + factorial n = |
| 166 | + match n |
| 167 | + | 0 -> 1 |
| 168 | + | n -> n * factorial (n - 1) |
| 169 | + end |
| 170 | + |
| 171 | + factorial2 = |
| 172 | + | 0 -> 1 |
| 173 | + | n -> n * factorial (n - 1), |
| 174 | + c = 3 |
| 175 | +end |
| 176 | + |
| 177 | + |
| 178 | +-- * -- |
| 179 | + |
| 180 | +def calculate_magic => |
| 181 | + let magic_number = 42 + fib another_regular_number, |
| 182 | + regular_number = 1, |
| 183 | + another_regular_number = 2 |
| 184 | + let add_one x => x + 1 |
| 185 | + let factorial => |
| 186 | + match n |
| 187 | + | 0 => 1 |
| 188 | + | n => n * factorial (n - 1) |
| 189 | + let fib n => |
| 190 | + | 0 | 1 => 1 |
| 191 | + | n => fib (n - 1) + fib (n - 2) |
| 192 | + in |
| 193 | + print ("The value of fib (magic_number + regular_number) is " |
| 194 | + ++ string (fib (magic_number + regular_number)) ++ "!") |
| 195 | + |
| 196 | + |
| 197 | +let calculate_magic = |
| 198 | + let magic_number = 42 + fib another_regular_number, |
| 199 | + regular_number = 1, |
| 200 | + another_regular_number = 2 |
| 201 | + let add_one x = x + 1 |
| 202 | + let factorial = |
| 203 | + match n |
| 204 | + | 0 => 1 |
| 205 | + | n => n * factorial (n - 1) |
| 206 | + end |
| 207 | + let fib |
| 208 | + | 0 | 1 => 1 |
| 209 | + | n => fib (n - 1) + fib (n - 2) |
| 210 | + end in |
| 211 | + print ("The value of fib (magic_number + regular_number) is " |
| 212 | + ++ string (fib (magic_number + regular_number)) ++ "!") |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | +def test () => |
| 218 | + let |
| 219 | + magic_number = 42, |
| 220 | + regular_number = 1, |
| 221 | + fun add_one x => x + 1, |
| 222 | + fun factorial => |
| 223 | + match n |
| 224 | + | 0 => 1 |
| 225 | + | n => n * factorial (n - 1) |
| 226 | + c = 3, |
| 227 | + fun fib n => |
| 228 | + | 0 | 1 => 1 |
| 229 | + | n => fib (n - 1) + fib (n - 2) |
| 230 | + in |
| 231 | + print "The value of a, f b, c are $a, $(f 4), $c!" |
| 232 | + |
| 233 | + |
| 234 | + |
| 235 | +-- StandardML style |
| 236 | +def test () => let |
| 237 | + val a = 1 |
| 238 | + |
| 239 | + fun f b = b + 1 |
| 240 | + |
| 241 | + fun factorial => |
| 242 | + match n |
| 243 | + | 0 -> 1 |
| 244 | + | n -> n * factorial (n - 1) |
| 245 | + end |
| 246 | + |
| 247 | + fun factorial2 => |
| 248 | + | 0 -> 1 |
| 249 | + | n -> n * factorial (n - 1), |
| 250 | + |
| 251 | + val c = 3 |
| 252 | +in |
| 253 | + print "The value of a, f b, c are $a, $(f 4), $c!" |
| 254 | + |
| 255 | + |
| 256 | +def test () => |
| 257 | + let |
| 258 | + a = 1 |
| 259 | + |
| 260 | + f b = b + 1 |
| 261 | + |
| 262 | + factorial => |
| 263 | + match n |
| 264 | + | 0 -> 1 |
| 265 | + | n -> n * factorial (n - 1) |
| 266 | + end |
| 267 | + |
| 268 | + factorial2 => |
| 269 | + | 0 -> 1 |
| 270 | + | n -> n * factorial (n - 1), |
| 271 | + |
| 272 | + c = 3 |
| 273 | + in |
| 274 | + print "The value of a, f b, c are $a, $(f 4), $c!" |
| 275 | + |
| 276 | + |
| 277 | +-- * -- |
| 278 | + |
| 279 | +module Person = ... |
| 280 | + |
| 281 | +interface Comparable = ... |
| 282 | + |
| 283 | +def hello name = ... |
| 284 | + |
| 285 | +object |
| 286 | + def say_hello () = |
| 287 | + print |
| 288 | +end |
| 289 | + |
| 290 | + |
| 291 | +-- * -- |
| 292 | + |
| 293 | +-- StandardML |
| 294 | +fun heron (a, b, c) = let |
| 295 | + val ab = dist (a, b) |
| 296 | + val bc = dist (b, c) |
| 297 | + val ac = dist (a, c) |
| 298 | + val perim = ab + bc + ac |
| 299 | + val s = perim / 2.0 |
| 300 | + in |
| 301 | + Math.sqrt (s * (s - ab) * (s - bc) * (s - ac)) |
| 302 | + end |
| 303 | + |
| 304 | + |
| 305 | +def heron a b c = |
| 306 | + let |
| 307 | + ab = dist (a, b) |
| 308 | + bc = dist (b, c) |
| 309 | + ac = dist (a, c) |
| 310 | + perim = ab + bc + ac |
| 311 | + s = perim / 2.0 |
| 312 | + in |
| 313 | + Math.sqrt (s * (s - ab) * (s - bc) * (s - ac)) |
| 314 | + end |
| 315 | + |
| 316 | +def heron a b c = |
| 317 | + Math.sqrt (s * (s - ab) * (s - bc) * (s - ac)) |
| 318 | + where |
| 319 | + ab = dist (a, b) |
| 320 | + bc = dist (b, c) |
| 321 | + ac = dist (a, c) |
| 322 | + perim = ab + bc + ac |
| 323 | + s = perim / 2.0 |
| 324 | + end |
| 325 | + |
| 326 | + |
| 327 | +-- * -- |
| 328 | + |
| 329 | +var x = 0 -- let x = ref 0 |
| 330 | + |
| 331 | +macro var $name = $val |
| 332 | + $name = ref $val |
| 333 | +end |
| 334 | + |
| 335 | +x = ref 0 |
| 336 | + |
| 337 | +x! |
| 338 | + |
0 commit comments