More details are also available on these two pages. (But some information is only discussed below; the others aren't supersets of this page.)

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.

Also, if you want to read code written outside this seminar, or have others read your code, for these reasons too you'll need to make the shift over to one of the established languages.

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.)

This is a complex document. We don't expect that you will be learning all of these languages simultaneously. But you may find it helpful to read through the whole thing to get a broad overview, then consult it more carefully about the language you're focused on learning at any given point. You may also find it helpful to consult when confronting code you don't understand in one of the other languages. There are important parts of these languages that aren't covered here, especially parts concerning types and monads and continuations, that we will be discussing later in the seminar. We will add additional Rosetta pages for those later. If you master the ideas summarized here, however, you will have a good understanding of the basic skeleton of each of these languages.

...  # this is a comment in Kapulet, that goes until the end of the line

...  ; this is a comment in Scheme, that goes until the end of the line

...  -- this is a comment in Haskell, that goes until the end of the line


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.

OCaml doesn't have comments of that sort. It only has "block" comments like this:

(* ... *)


which may last for several lines. These comments nest, so that:

(* ... (* inner *) ... *)


is a single comment.

Haskell also has block comments, though it {- writes them differently -}. Haskell's block comments also nest.

Racket and Scheme also have block comments, though they #| write them differently |#. 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 only recently became part of the official Scheme standard, but they have been widely implemented.)

### Variables

Our syntax for variables in Kapulet is close to that in the other languages. Haskell and OCaml differ only in that they do not permit trailing ? or !; however, they do permit trailing 's (and even permit 's in the middle of a variable too, which Kapulet does not). Scheme permits all of these characters, plus many more punctuation symbols as well, to occur anywhere in a variable. Scheme also permits variables to begin with capital letters, or to consist solely of the single character _; but the other languages reserve these terms for special purposes.

### Case, Cond, and If ... then ...

The complex expression that's written like this in Kapulet:

# Kapulet
case some_expression of
0 then result0;
1 then result1;
x then resultx
end


is written very similarly in Haskell:

-- Haskell
case some_expression of {
0 -> result0;
1 -> result1;
x -> resultx
}


Unlike the other languages we're discussing, Haskell pays special attention to the whitespace/indentation of what you write. If you've got the indentation right, you can omit the {, ;, and }s in the above. And that's how you will often see Haskell code displayed. On this website, though, I propose to always include the {s and so on when displaying Haskell code, because the indentation rules aren't 100% intuitive. It's easy to read properly-indented Haskell code, but until you've learned and practiced the specific rules, it's not always easy to write it.

The case construction is written only a little bit differently in OCaml:

(* OCaml *)
match some_expression with
0 -> result0 |
1 -> result1 |
x -> resultx


Note there is no closing end or }. You can enclose the whole expression in parentheses if you want to, and when embedding it in some larger expressions (like another match expression), you may need to. Sometimes the | dividers are written at the start of a line, and you are allowed to include an extra one before the first line, so you could also see this written as:

(* OCaml *)
match some_expression with
| 0 -> result0
| 1 -> result1
| x -> resultx


The syntax for guards and as-patterns also only varies slightly between these languages:

# Kapulet
case some_expression of
pat1   when guard             then result1;
pat1   when different_guard   then result2;
((complex_pat) as var, pat4)  then result3
end


-- Haskell
case some_expression of {
pat1 | guard              -> result1;
| different_guard    -> result2;
(var@(complex_pat), pat4) -> result3
}

(* OCaml *)
match some_expression with
pat1   when guard             -> result1 |
pat1   when different_guard   -> result2 |
((complex_pat) as var, pat4   -> result3


The official Scheme standard only provides for a limited version of this. There is a case construction, available since at least "version 5" of the Scheme standard (r5rs), but it only accepts literal values as patterns, not any complex patterns containing them or any patterns containing variables. Here is how it looks:

; Scheme
(case some_expression
((0) 'result0)
((1) 'result1)
((2 3 5) 'smallprime)
(else 'toobig))


The results can be complex expressions; I just used bare symbols here for illustration. Note that the literal patterns in the first two clauses are surrounded by an extra pair of parentheses than you might expect. The reason is shown in the third clause, which begins (2 3 5). This does not mean to match a list containing the values 2 3 and 5. Instead it means to match the simple value 2 or the simple value 3 or the simple value 5. The final else clause is optional. If it's omitted, and none of the other clauses match, the result is Scheme's special void value.

The patterns here can be any literal value (what the Scheme standards call a "datum"). Numbers are permitted, as are boolean literals (#t and #f) and symbolic atoms ('alpha and the like, though inside a pattern position in a case construction, you omit the initial '). You can also use the list literal '() (again, omit the initial ' when writing it as a pattern). Some implementations of Scheme allow more complex list patterns, matching literal lists like '(alpha 0 () #t); others don't.

There are various add-on libraries to Scheme that will permit you to pattern-match in more ambitious ways, approximating what you can do in Kapulet, OCaml, and Haskell. We will explain some of these later in the course, after we've introduced you to the notion of datatypes.

What programmers using standard Scheme tend to do instead is to use predicates that query the type and/or structure of an unknown value, and then take separate evaluation paths depending on the result. This can be done with an if ... then ... else ... construction, or with Scheme's more general cond construction. In Scheme, these two are equivalent:

; Scheme
(if test1 'result1                    ; else what follows:
(if test2 'result2          ; else what follows:
(if test3 'result3 'somethingelse)))

(cond
(test1 'result1)
(test2 'result2)
(test3 'result3)
(else  'somethingelse))


The tests tend to use predicates like null? (are you the empty list?), pair? (are you a non-empty list, whether proper or improper?), list? (are you a proper list, whether empty or not?), symbol?, boolean?, number?, zero? (you get the idea). The Little Schemer books use their own predicates they call atom? (are you a non-list?) and lat? (are you a list all of whose members are atoms?)

You can also use more complex tests you write on the spot, or your own antecedently-defined functions:

; Scheme...in case the parens left any doubt
(define smallprime? (lambda (x) (if (= x 2) #t (if (= x 3) #t (if (= x 5) #t #f)))))

(cond
((= x 0) 'infant)
((smallprime? x) 'myfavorite)
((and (> x 10) (< x 20)) 'teenaged)
(else 'unknown))


Remember that in Scheme, an expression doesn't have to evaluate to #t to be treated as "truth-like". Every value other than #f is treated as truth-like. As I said before (if 0 'zero 'nope) evaluates to 'zero.

You may sometimes see Scheme cond constructions written with this kind of clause:

(cond
...
(test-expression => function-value)
...)


That's the same as the following:

(cond
...
(test-expression (function-value test-expression))
...)


Except that it only evaluates the test-expression once.

The clauses in Scheme's cond expressions can contain multiple expressions after the test. This only becomes useful when you're working with mutable values and side-effects, which we've not gotten to yet. The if expressions only take a single expression for the "then" branch and a single expression for the "else" branch. You can turn a complex series of expressions, which may involve side-effects, into a single expression by wrapping it in a (begin ...) construction. The (begin ...) construction as a whole evaluates to whatever the last expression it contains does.

Scheme standards after r5rs also provide two further conditional constructions, which are for the situations where you want to perform a meaningful action only on the "then" branch, or only on the "else" branch:

(when test-expression
result-expression1...)

(unless test-expression
result-expression2...)


If the test-expression evaluates to #f, then the when expression evaluates to Scheme's special void value; mutatis mutandis for the unless expression. This is analogous to () in OCaml, Haskell, and Kapulet.

In the last three languages, the expressions in the then-branch and the else-branch of a conditional have to have the same type. You can't say if test-expression then 0 else []. Also, they expect the test-expression to evaluate specifically to a boolean value, not merely to 'false versus "anything else". They are stricter about types here than Scheme is.

In the special case where an else-branch evaluate to () (and thus so too must the then-branch), and the else-branch does so using no complex expression but merely the literal (), then OCaml permits you to omit that else-branch. So in OCaml you can write this:

 if test_expression then then_result


 if test_expression then then_result else ()


This is similar to Scheme's when construction. Kapulet and Haskell have no analogue.

### Lambda expressions

In Kapulet you write λ expressions (sometimes called "anonymous functions") with a prefix of either λ or the spelled-out lambda. That's followed by one or more patterns, separated by spaces, then a period, then a single expression which makes up the body of the function. When there are multiple patterns, the function expressed is curried, thus:

lambda (x, y) z. result


means the same as:

lambda (x, y). (lambda z. result)


The parentheses could have been omitted around lambda z. result; they're just there to focus your attention.

Haskell and OCaml are very similar to this, they just use some slightly different notation. In Haskell you'd write:

-- Haskell
\(x, y) z -> result


and in OCaml you'd write:

(* OCaml *)
fun (x, y) z -> result


You may sometimes see λ expressions in OCaml written using function instead of fun. These overlap somewhat in their usage. The difference is that function only allocates a position for one argument pattern, so can't straightforwardly define curried functions. (You can however embed function expressions inside other function expressions.) On the other hand, function can take multiple variant patterns for that single position. Thus with function you can say:

(* OCaml *)
function []    -> result1 |
x::xs -> result2


whereas with fun you'd have to write:

(* OCaml *)
fun ys -> match ys with
[]    -> result1 |
x::xs -> result2


In Scheme, lambda expressions are written like this:

; Scheme
(lambda (vars...) body-expressions...)


Scheme only permits simple variables as its argument patterns, and the lambda expression can be defined to take zero or more arguments:

; Scheme
(lambda () ...)
(lambda (x) ...)
(lambda (x y) ...)
(lambda (x y z) ...)


As I said before, we will discuss functions that "take zero arguments" a few weeks into the seminar.

There is special syntax for defining functions that may take varying numbers of arguments (recall and and +), where Scheme binds a single variable to a list containing all of the received arguments (or all of the arguments after the nth...). I won't explain that syntax here.

### Let, Letrec, and Define

Kapulet has the syntax:

# Kapulet
let
pat1  match expr1;
pat2  match expr2;
pat3  match expr3
in result


which is equivalent to:

# Kapulet
let
pat1  match expr1
in let
pat2  match expr2
in let
pat3  match expr3
in result


There is also a corresponding letrec form. In let, the bindings in pat1 are in effect for the evaluation of all of expr2, expr3, and result (but not any further, if this is part of a more complex expression); similarly for the bindings in pat2 and pat3. In letrec, all of the bindings on the left-hand side are in effect for all of the right-hand side expressions, as well as for the result.

OCaml only has the second, more verbose form of this, and writes it a bit differently:

(* OCaml *)
let
pat1  = expr1
in let
pat2  = expr2
in let
pat3  = expr3
in result


If you want to define some mutually recursive functions with letrec, OCaml uses a special syntax for that, using letrec ... and ... in ...:

(* OCaml *)
letrec
even  = fun x -> if x = 0 then true else odd x
and
odd   = fun x -> if x = 0 then false else even x
in ...


Haskell has both of the syntactic forms that Kapulet does, though like OCaml, it uses = rather than match. And it wraps all the binding clauses with { ... } (see earlier remarks about Haskell and whitespace/indentation):

-- Haskell
let {
pat1  = expr1;
pat2  = expr2;
pat3  = expr3
} in result


Also, in Haskell let always means letrec. There is no term in Haskell that means what simple let does in Kapulet and OCaml.

Haskell also has another form, roughly synonymous with its let ... in .... It looks like this:

-- Haskell
result where {
pat1  = expr1;
pat2  = expr2;
pat3  = expr3
}


Here all the new bindings introduced for the variables in the pats are in effect for the evaluation of the exprs (this works like letrec too), and also for the evaluation of result.

There are a few places where you can use let ... in ... but not ... where ..., and a few places where the inverse is true.

Scheme has four (or five) syntactic forms here, including let, let*, letrec, and letrec*. The difference between the last two is subtle and only arises in the presence of continuations; you can just use letrec for ordinary purposes. I won't try to explain the difference between let and let* here, except to say this:

1. When there's only a single pattern-binding clause, as in (let ((var expression)) result), let and let* work the same.
2. When there are multiple pattern-binding clauses, as in (let ((var1 expression1) (var2 expression2)) result), then they work somewhat differently and let* is probably the one that works like you're expecting.

The let* form is the one that corresponds to let in Kapulet. I recommend you get in the habit of just always using let* (or letrec) in Scheme, instead of let.

When you're at the "toplevel" of your program, or of a library/module/compilation-unit (the terminology differs), there is also another syntactic form possible. In Kapulet, you'd write:

# Kapulet
let
pat1  match expr1;
...
end
... # rest of program or library


Notice that this form ends with end, not with in result. The above is roughly equivalent to:

# Kapulet
let
pat1  match expr1;
...
in ... # rest of program or library


That is, the bindings initiated by the clauses of the let construction remain in effect until the end of the program or library. They can of course be "hidden" by subsequent bindings to new variables spelled the same way. The program:

# Kapulet
let
x  match 0
end
let
x  match 1
end
x


evaluates to 1, just like:

# Kapulet
let
x  match 0
in let
x  match 1
in x


does. There's a similar form for letrec.

OCaml can do the same:

let
x = 0 ;;
let
x = 1 ;;
x


The double-semicolons are hints to OCaml's "toplevel interpreter" that a syntactic unit has finished. In some contexts they're not needed, but it does no harm to include them if you're not sure.

Haskell's "toplevel interpreter" (ghci) permits a syntactic form that looks superficially quite like these:

let x = 2
x


but under the covers something quite different is happening. (Specifically, you're working "inside the IO Monad", except that in this special context, expressions like x that don't evaluate to monadic values are permitted and evaluated. We don't expect that you will understand yet what any of this means.) If you're writing in a file that you want Haskell to interpret or compile, on the other hand, you have to do something a bit different (which you can't easily also do at the toplevel in ghci). Recall the shortcut by which we permitted:

# Kapulet
let
f  match lambda pat1. body1;
g  match lambda pat2 pat3. body2
in ...


to be written more concisely as:

# Kapulet
let
f pat1      = body1;
g pat2 pat3 = body2
in ...


OCaml and Haskell permit that same shorthand. And Haskell additionally permits the bare binding clauses of such expressions (that is, without the surrounding let and in) to occur at the toplevel of files. In other words, a Haskell file can look like this:

-- Haskell file.hs
f pat1      = body1

g pat2 pat3 = body2
...


Note there are no semicolons here. These are called "toplevel declarations" of the functions f and g. A single function name can have multiple declarations (within a single scoping context), using different patterns:

-- Haskell file.hs
f [] = 0
f (x:xs) = 1 + f xs


defines f as a function that returns the length of a single List argument. (You can also do that inside Haskell's let constructions, too.) This is what corresponds in Haskell files to let ... end in Kapulet.

Haskell also permits multiple declarations of this sort inside its let and where constructs, too. Moreover, these declarations can also have pattern guards, as in:

-- Haskell file.fs
f [] = 0
f (x:xs) | odd x = 1 + f xs
| otherwise = f xs


Scheme has a version of letrec ... end, which it writes as define. Thus in Scheme this:

; Scheme
(define var1 expr1)
... ; rest of program


evaluates the same as this:

; Scheme
(letrec ((var1 expr1))
... ; rest of program
)


This is what we can call Scheme's fifth form of the let family.

Some versions of Scheme permit you also to include define inside some (but not all) complex expressions. Thus you can write:

(lambda (x)
(define var1 expr1)
...)


(lambda (x)
(letrec ((var1 expr1))
...))


There is no analogue to this in the other languages.

### Further Installments ...

We will expand these comparisons (on separate web pages) as we introduce additional ideas in the course, such as types and monads and continuations.

## Why did you name these pages "Rosetta"?

The 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:

 Scheme OCaml Haskell Rosetta Code Rosetta Code Rosetta Code PLEAC PLEAC PLEAC n/a langref.org code codex code codex code codex 99 problems 99 problems 99 problems