huh, last edits are not displaying
[lambda.git] / rosetta.mdwn
1 [[!toc levels=2]]
2
3 ** *This page is still being written!* **
4
5
6 ## Can you summarize the differences between your made-up language and Scheme, OCaml, and Haskell? ##
7
8 The made-up language we wet our toes in in week 1 is called Kapulet. (I'll tell you the story behind its name sometime.) The purpose of starting with this language is that it represents something of a center of gravity between Scheme, OCaml, and Haskell, and also lacks many of their idiosyncratic warts. One downside is that it's not yet implemented in a form that you can run on your computers. So for now, if you want to try out your code on a real mechanical evaluator, you'll need to use one of the other languages.
9
10 Also, if you want to read code written outside this class, or have others read your code, for these reasons too you'll need to make the shift over to one of the established languages.
11
12 We hope, though, that learning Kapulet first puts you in a position to make that shift more effortlessly, and also to more quickly see the places where there's underlying unity to Scheme, OCaml, and Haskell, despite their diverse syntaxes. (And idiosyncratic warts.)
13
14 ### Comments
15
16     ...  # this is a comment in Kapulet, that goes until the end of the line
17
18     ...  ; this is a comment in Scheme, that goes until the end of the line
19
20     ...  -- this is a comment in Haskell, that goes until the end of the line
21
22 Note that for Haskell's comments, the `--` must be immediately followed by something like a space or a letter. `-->` does not begin a comment; it's a legal operator symbol.
23
24 OCaml doesn't have comments of that sort. It only has "block" comments like this:
25
26     (* ... *)
27
28 which may last for several lines. These comments *nest*, so that:
29
30     (* ... (* inner *) ... *)
31
32 is a single comment.
33
34 Haskell also has block comments, though it `{- writes them differently -}`.
35 Haskell's block comments also nest.
36
37 Racket and Scheme also have block comments, though they `#| write them differently |#`.
38 These block comments also nest. Another form of block comments is `#;( ... )`. Those may contain nested parentheses, and extend until the next *matching* `)`. So prefixing `#;` to a complex parenthesized expression is a way to turn the whole thing into a comment. (These two comment styles aren't part of the official Scheme standard, but they are widely implemented.)
39
40
41
42 ### Variable names
43
44 % Haskell's division into letter-based vs operators. Each can be "flagged" to temporarily behave as though it belonged to the other syntactic category (see below).
45
46
47
48 ### Infix operators and parentheses
49
50
51 Kapulet, OCaml, and Haskell all understand some expressions like `+` to be infix operators. So you would write:
52
53     1 + 2
54
55 not:
56
57     + 1 2
58
59 Although all three of these languages permits you to enclose an infix operator in parentheses to make a *section*, which no longer has infix syntax. In Kapulet, `( + )` is the same as λ `(x, y). x + y`, whereas in OCaml and Haskell it's a *curried* function, which we can write (in Kapulet syntax) as λ `x y. x + y`.
60
61 Kapulet and OCaml have some operators spelled with letters also taking infix syntax, such as `comp` in Kapulet or `mod` in OCaml. In Haskell, this is never the case: variables that are made of letters are only treated as function terms being applied to arguments *when they're at the start* of a list of expressions; and variables that are made of punctuation symbols, and not enclosed in parentheses, will only be treated as infix operators. However, Haskell permits you to temporarily "flag" a letter-made function term to behave like an infix operator, by enclosing it in `` ` `` marks. Thus in Haskell you can write:
62
63     3 `mod` 2
64
65 But without the `` ` ``, you'd have to write: `mod 3 2`.
66
67 Scheme has no infix operators. It ruthlessly demands that all functions to be applied to arguments come at the start of a list of expressions, regardless of whether those functions are spelled using letters, punctuation symbols, or a mix of the two. Thus in Scheme one always writes:
68
69     (+ 3 2)
70
71 and the like. Moreover, in Scheme parentheses are never optional and never redundant. In contexts like this, the parentheses are necessary to express that the function is being applied; `+ 3 2` on its own is not a complete Scheme expression. And if the `+` were surrounded by its own parentheses, as in:
72
73     ((+) 3 2)
74
75 what that would mean is that `+` is first being applied to *zero* arguments, which is different from not applying it all. (In Kapulet, OCaml, and Haskell, one would write that `f` is being applied to "zero arguments" like this: `f ()`.) Scheme helpfully defines the result of applying `+` to zero arguments to be `0`. So `((+) 3 2)` would evaluate to whatever `(0 3 2)` does, and that's an error, because `0` is not a function.
76
77 Note that `(0 3 2)`, although it *is*, qua expression, a list of numbers, does not evaluate to a list. To get an expression that *evaluates to* that list, you'd have to use `(list 0 3 2)` or `'(0 3 2)`. (Notice the initial `'`.)
78
79 In Scheme, you can also write `(+ 3 2 10)`, and so on. You only have to write `(+ (+ 3 2) 10)` if you really want to.
80
81 % Parentheses have other roles in Scheme, too.
82
83 In Scheme, the default style for defining functions is as taking several arguments simultaneously, that is the *uncurried* style. In OCaml and Haskell, the default style is to define them *curried*. Curried functions can easily be partially applied:
84
85     (* OCaml *)
86     let add  = fun x y -> x + y in
87     let add2 = add 2 in
88         add2 3
89     ;;
90
91 will result in `5`.
92
93 In Scheme, the default would be to define `add` like this:
94
95     (define add (lambda (x y) (+ x y)))
96
97 Then you cannot say `(add 2)`, because `add` will be expecting two arguments, but you only supplied one. You can however define curried functions in Scheme, it's just more laborious:
98
99     (define curried_add (lambda (x) (lambda (y) (+ x y))))
100     (define add2 (curried_add 2))
101     (add2 3)
102
103 will result in `5`. This is the best one can do in official Scheme, but there are various syntax extensions and macros out there to make it possible to write this sort of thing more succinctly.
104
105 OCaml and Haskell also permit defining functions in uncurried form:
106
107     (* OCaml *)
108     let add  = fun (x, y) -> x + y in
109     let add2 = fun add 2 in ...
110
111 Here the last displayed line will fail, because `add` expects as its argument a tuple of two numbers.
112
113 Kapulet essentially works like OCaml and Haskell; though for pedagogical reasons I started out by introducing uncurried definitions, rather than the *curried* definitions those other languages predominantly use.
114
115 As we mentioned, in Kapulet, OCaml, and Haskell, there is a shorthand that enables you to write things like:
116
117     # Kapulet
118     let
119       ten_minus match lambda x. 10 - x;
120       and_ys    match lambda x. x & ys
121     in (ten_minus, and_ys)
122
123 like this:
124
125     # Kapulet
126     let
127       ten_minus match (10 - );
128       and_ys    match ( & ys)
129     in (ten_minus, and_ys)
130
131 There are just minor differences between these languages. First, OCaml doesn't have the `( + 10)` or `(10 + )` forms, but only the `( + )`. Second, as a special case, OCaml doesn't permit you to do this with its list-consing operator `::`. You have to write `fun x xs -> x :: xs`, not `( :: )`. Whereas in Kapulet `( & )`, `(x & )`, and `( & xs)` are all sections using its sequence-consing operator `&`; and in Haskell, `( : )`, `(x : )`, and `( : xs)` are the same.
132
133 Thirdly, in Kapulet, `( - 10)` also expresses λ `x. x - 10` (consistently with `(10 - )`), but Haskell (and OCaml) treat this form differently, and interpret it as meaning the integer `- 10`. Here's how to express some things in Kapulet:
134
135     # Kapulet
136     (0 - 2)
137     ( - 2)         # ( - 2) 10 == 8
138     (0 - )
139     ( - ) (5, 3)
140     
141
142 and here are their translations into Haskell:
143
144     -- Haskell
145     ( -2 )
146     (subtract 2)  -- subtract 2 10 == 8
147     negate        -- (0 - ) also works
148     ( - ) 5 3
149
150 OCaml expresses `(0 - )` or `negate` as `~-`. You can write `3 * (0 - 2)` in OCaml as either `3 * ( -2 )` or as `3 * ~-2`.
151
152 I know all these languages fairly well, and I still find this last issue difficult to keep track of. You may be starting to understand why I spoke of "warts."
153
154
155 ### Equality and Booleans
156
157 The relation that's written `==` in Kapulet is also written that way in Haskell. That symbol means something else in OCaml, having to do with mutable reference cells; to get the same notion in OCaml one writes just a single `=`. The negation of this notion is written `!=` in Kapulet, `/=` in Haskell, and `<>` in OCaml. (Again, `!=` means something else in OCaml.)
158
159 The relations that are written `and`, `or`, and `not` are written in Haskell and OCaml as `&&`, `||`, and `not`. (Haskell uses `and` and `or` to express functions that form the conjunction or disjunction of every `Bool` value in a List of such. OCaml permits `or` as an old synonym for `||`, but discourages using that spelling. OCaml also permits `&` as an old, discouraged synonym for `&&`.)
160
161 Scheme %%
162
163 The values that are written `'true` and `'false` in Kapulet are written in Haskell as `True` and `False`, and in OCaml as just `true` and `false`. They're written `#t` and `#f` in Scheme, but in Scheme in many contexts any value that isn't `#f` will behave as though it were `#t`, even values you might think are more "false-ish", like `0` and the empty list. Thus `(if 0 'yes 'no)` will evaluate to `'yes`.
164
165 Scheme recognizes the values `'true` and `'false`, but it treats `'false` as distinct from `#f`, and thus as a "true-ish" value, like all of its other values that aren't `#f`.
166
167
168
169 ### Sequences, Lists, and Tuples
170
171 In Kapulet, we have a notion I called a "sequence" which has an empty form `[]` and a cons-ing operator `&`, so that:
172
173     1 & 2 & 3 & []
174
175 can also be written:
176
177     [1, 2, 3]
178
179 Haskell is very similar, except that it calls these Lists, and its cons-ing operator is written `:`. OCaml also calls them `list`s, and its cons-operator is written `::`. (OCaml *also* uses `:`, but it uses it to deal with types, and Haskell in turn also uses `::`, but that's what *it* uses to deal with types. Grr.)
180
181 Kapulet writes the operator that concatenates or appends sequences as `&&`. Thus:
182
183     # Kapulet
184     [1, 2] && [3, 4, 5]
185
186 evaluates to `[1, 2, 3, 4, 5]`. Haskell writes this operator as `++`. In Haskell, a `String` is just a List of `Char`, so `++` is also the operator we use to append strings:
187
188     -- Haskell
189     "over" ++ "due"
190
191 evaluates to `"overdue"`. In OCaml, `string`s aren't implemented as `list`s, so their append operators are different: `^` for `string`s and `@` for `list`s:
192
193     (* OCaml *)
194     [1; 2] @ [3; 4; 5] ;;
195     "over" ^ "due" ;;
196
197 evaluate to `[1; 2; 3; 4; 5]` and `"overdue"`. Note that OCaml separates its `list` items with semicolons not commas. If you write `[1, 2, 3]` in OCaml, it will think that's a one-element list whose first element is a triple, that is, what you'd write in Haskell as `[(1, 2, 3)]`.
198
199 Here are some list functions in Kapulet:
200
201     length
202     (&&)
203     # the following were defined in homework
204     empty?
205     tail
206     drop
207     take
208     split
209     filter
210     partition
211     map
212     map2
213     # the following were defined in extra credit
214     unmap2
215     takewhile
216     dropwhile
217     reverse
218     # new functions
219     concat       # converts [[10, 20], [30], [], [40, 50]]
220                  # to [10, 20, 30, 40, 50] (only merging a single layer of []s)
221     (mem)        # infix syntax, 2 mem [1, 2, 3] == 'true
222     nth          # nth [10, 20, 30] 1 == 20, because the first element
223                  # is at position 0; fails if index is out of bounds
224     all? p xs    # all? odd? [1, 3, 5] == 'true
225     any? p xs    # any? even? [1, 3, 5] == 'false
226
227
228
229 Here are the corresponding functions in Haskell:
230
231     length
232     (++)
233     null
234     tail     -- compare head, which fails on []
235     drop     {- but these are curried functions, so you write `drop n xs`
236                 not `drop (n, xs)` as in Kapulet -}
237     take
238     splitAt
239     filter
240     Data.List.partition
241     map
242     zipWith  {- zip handles the special case where f is the function that forms ordered pairs
243                 both zipWith and zip stop with the shortest list -}
244     unzip    -- doesn't take an f argument, assumes (\(x, y) -> (x, y))
245     takeWhile
246     dropWhile
247     reverse
248     concat
249     elem     -- not infix syntax, but often written as: 2 `elem` [1, 2, 3]
250     (!!)     -- infix syntax: [10, 20, 30] !! 1 == 20; fails if index is out of bounds
251     all p xs
252     any p xs
253
254
255
256 Here they are in OCaml:
257
258     length
259     (@)          (* or List.append *)
260     (* no function corresponding to empty? *)
261     List.tl      (* compare List.hd, which fails on [] *)
262     (* no function corresponding to drop or take *)
263     (* no function corresponding to split; OCaml uses List.split to mean something else *)
264     List.filter  (* also List.find_all *)
265     List.partition
266     List.map
267     List.map2    (* compare List.combine, like Haskell's zip
268                     both map2 and combine fail if the lists are different lengths *)
269     List.split   (* like Haskell's unzip, doesn't take an f argument *)
270     (* no function corresponding to takewhile or dropwhile *)
271     List.rev
272     List.concat  (* also List.flatten, which still only merges a single layer of []s *)
273     List.mem     (* not infix syntax *)
274     List.nth     (* List.nth [10; 20; 30] 1 = 20; fails if index is out of bounds *)
275     List.for_all p xs
276     List.exists p xs
277
278
279 How does all this look in Scheme? Well, Scheme has a notion they call a (proper) `list`, and also a notion they call a `vector`. There are also what Scheme calls "improper" `list`s, with `(cons 1 'nonlist)` or `'(1 . nonlist)`, where `'nonlist` is any non-list (here it's a `symbol`) being a special case. Let's ignore the improper `list`s. Scheme's (proper) `list`s and `vector`s each has a claim to correspond to Kapulet's sequences, Haskell's Lists, and OCaml's `list`s. Each is also somewhat different. The dominant differences are:
280
281 1.  these structures in Scheme can contain heterogenously-typed elements, including further `list`s and `vector`s in some positions but not in others
282 2.  in the official Scheme standard, `list`s and `vector`s are both *mutable* containers, that is, one and the same persisting `list` structure can have different
283 elements at different stages in a program's evaluation
284
285 Many Scheme implementations also provide immutable versions of `list`s and `vector`s, more closely approximating the sequences/lists in Kapulet, Haskell, and OCaml. With some configurations, Racket even makes the immutable versions the defaults. But none of these are yet part of the official Scheme standard. Also, difference 1 is present in all Scheme implementations. This makes Scheme's `list`s and `vector`s in some ways more akin to *tuples* in the other languages (to "proper" tuples in Kapulet).
286
287 There are also some differences in how `list`s are specified in Scheme versus the other languages. In Scheme, one writes the empty list like this:
288
289     (list)
290
291 and lists with more elements like this:
292
293     (list 10)
294     (list 10 x)
295     (list 10 x 'alpha)
296     (list 10 x 'alpha (list 'beta 'gamma) 'delta 20)
297
298 In the preceding, the `x` is a variable and is evaluated to be whatever value it's bound to in the context where the displayed expressions are being evaluated. If one has a list specification that contains no variables, no matter how deeply embedded, then a certain shorthand becomes available, using a `'` prefix, like this:
299
300     '()                          ; same as (list)
301     '(10)                        ; same as (list 10)
302     '(10 alpha)                  ; same as (list 10 'alpha)
303     '(10 alpha (beta gamma) 20)  ; same as (list 10 'alpha (list 'beta 'gamma) 20)
304
305 Scheme can also write <code>'<em>something</em></code> as <code>(quote <em>something</em>)</code>. (The `quote` is not a function being applied to some argument; this is a special syntax that only superficially *looks* like a function application.)
306
307
308 Here are the `list` functions in Scheme corresponding to the functions listed in the other languages:
309
310     cons              ; corresponds to Kapulet's ( & ), Haskell's ( : ), OCaml's `::`
311     length
312     append            ; corresponds to Kapulet's ( && ), Haskell's ( ++ ), OCaml's ( @ )
313                       ; can be applied to one or more arguments
314     null?             ; corresponds to Kapulet's empty?, Haskell's null
315     car               ; corresponds to Haskell's head
316     cdr               ; corresponds to Kapulet's and Haskell's tail
317     (list-tail xs k)  ; corresponds to Kapulet's drop (k, xs); fails if out-of-bounds
318     ; no official function corresponding to take or split or filter or partition
319     map               ; corresponds to Kapulet's map and map2
320                       ; can take one or more list arguments
321     ; no official function corresponding to unmap2 or takewhile or dropwhile
322     reverse
323     ; no official function corresponding to concat
324     member            ; corresponds to Kapulet's (mem) and Haskell's elem
325     (list-ref xs k)   ; corresponds to Kapulet's `nth xs k`
326     ; no official function corresponding to all or any
327
328 All of the functions listed as missing from the official Scheme standard can be found in various add-on libraries, or you could define them yourself if you had to.
329
330
331
332
333
334 <!--
335
336 symbol=?
337
338 characters: #\c  #\xff  #\space  #\newline
339
340
341
342 ### Other functions
343
344 Same in all: `succ`, `pred`, `fst`, `snd`.
345
346 Same in Kapulet and Haskell (modulo the differences between multivalues and tuples), aren't predefined in OCaml: `id`, `const`, `flip`, `curry`, `uncurry`.
347
348 Kapulet's `(comp)` is Haskell's `( . )`; isn't predefined in OCaml.
349
350 Kapulet and Haskell both have `( $ )`; OCaml expresses as `( @@ )`. (OCaml also has `|>` to express the converse operation: `f x`, `f @@ x` and `x |> f` all mean the same.)
351
352 Kapulet's `odd?` and `even?` are Haskell's `odd`, `even`; aren't predefined in OCaml.
353
354 Kapulet's `swap` (defined in homework) is Haskell's `Data.Tuple.swap`.
355
356 Kapulet's `dup` isn't predefined in Haskell but can be easily expressed as `\x -> (x, x)`.
357
358 -->
359
360
361
362 ### More to come ...
363
364 (This page is being worked on...)
365
366 FIXME
367
368
369 ## Offsite Readings comparing Scheme, OCaml, and Haskell ##
370
371
372 *   [Haskell for OCaml Programmers](http://science.raphael.poss.name/haskell-for-ocaml-programmers.pdf)
373 *   [Introduction to OCaml for Haskellers](http://foswiki.cs.uu.nl/foswiki/pub/Stc/BeyondFunctionalProgrammingInHaskell:AnIntroductionToOCaml/ocaml.pdf), [another](http://blog.ezyang.com/2010/10/ocaml-for-haskellers/)
374 *   Haskell Wiki on [OCaml](https://wiki.haskell.org/OCaml)
375 *   [ML Dialects and Haskell](http://hyperpolyglot.org/ml)
376 *   [Differences between Haskell and SML?](http://www.quora.com/What-are-the-key-differences-between-Haskell-and-Standard-ML?browse)
377 *   [Comparing SML to OCaml](http://www.mpi-sws.org/~rossberg/sml-vs-ocaml.html)
378
379
380
381
382 ## Why did you name these pages "Rosetta"? ##
383
384 The [Rosetta Stone](https://en.wikipedia.org/wiki/Rosetta_Stone) is a famous slab discovered during Napoleon's invasion of Egypt, that had the same decree written in ancient Greek (which modern scholars understood) and two ancient Egyptian scripts (which they didn't). The slab enabled us to recover understanding of those Egyptian scripts; and has since come to be a symbol for the simultaneous expression of a single idea in multiple languages. A number of websites do this for various programming languages:
385
386 <table><th>
387 <td>Scheme
388 <td>OCaml
389 <td>Haskell
390 <tr>
391 <td rowspan=10>&nbsp;
392 <td><a href="http://rosettacode.org/wiki/Category:Scheme">Rosetta Code</a>
393 <td><a href="http://rosettacode.org/wiki/Category:OCaml">Rosetta Code</a>
394 <td><a href="http://rosettacode.org/wiki/Category:Haskell">Rosetta Code</a>
395 <tr>
396 <td><a href="http://pleac.sourceforge.net/pleac_guile/index.html">PLEAC</a>
397 <td><a href="http://pleac.sourceforge.net/pleac_ocaml/index.html">PLEAC</a>
398 <td><a href="http://pleac.sourceforge.net/pleac_haskell/index.html">PLEAC</a>
399 <tr>
400 <td>n/a
401 <td colspan=2 align=center><hr><a href="http://langref.org/ocaml+haskell/solved">langref.org</a>
402 <tr>
403 <td><a href="http://www.codecodex.com/wiki/Category:Scheme">code codex</a>
404 <td><a href="http://www.codecodex.com/wiki/Category:Objective_Caml">code codex</a>
405 <td><a href="http://www.codecodex.com/wiki/Category:Haskell">code codex</a>
406 <tr>
407 <td><a href="http://community.schemewiki.org/?ninety-nine-scheme-problems">99 problems</a>
408 <td><a href="http://ocaml.org/learn/tutorials/99problems.html">99 problems</a>
409 <td><a href="https://wiki.haskell.org/H-99:_Ninety-Nine_Haskell_Problems">99 problems</a>
410 </table>
411
412 See also the [Project Euler](https://projecteuler.net/) programming challenges.