Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
0 of .
Results for:
P. 1
Programming Language Theory Notes

# Programming Language Theory Notes

Ratings:
(0)
|Views: 21|Likes:
Class notes taken from the spring 2013 semester of the graduate level programming language course at Cornell University
Class notes taken from the spring 2013 semester of the graduate level programming language course at Cornell University

Categories:Types, School Work

### Availability:

See more
See less

04/29/2014

pdf

text

original

CS 6110: Ocaml Tutorial
Lee GaoJanuary 25, 2013
1. Binding
1
let
sixtyOneTen = 6110
2. Tuples and Datatypes
1
let
t1 : int
int = (3 ,5)
2
let
t2 : string
bool
char = (”moo, true , ’q ’)
3
let
t3 : unit = ()
1
type
|
Diamonds
| · · ·
3. Pattern Matching
1
let
t1 : int
int =
...
23
let
(a ,b) = t1
45
let
( ,b) = t1
67
let
(a ,5) = t1
Can match constants too ; incomplete
)
4. More pattern matching, with custom types
1
let
suitname : string =
2
match
c
with
3
>
|
Diamonds
>
”diamonds”
4
|
...
If you don’t exhaustive specify all possibilities,
OCaml
will warn you.
1
let
b : bool =
...
2
match
b
with
3
true
> ...
4
|
false
> ...

1
let
pair = (42 ,611)
2
let
x
= 611
3
match
pair
with
4
(
x
,611)
>
0
5
|
(42 ,x)
>
1
6
|
>
2
1

let
square : int
>
int =
2
fun
(x: int )
>
x
x
3
iso
=
4
let
suqare (x: int ) : int = x
x
7. Recursive functions must have a
rec
annotation to tell top level to binditself into tis environment
1
let rec
x
! =
2
i
x = 0
then
1
3
else
x
((
x
1)!)
8. Tuple argument (using CHI is isomorphic)
1
let
f (a ,b) : int =
2
...
34
let
f (p: int
int ) : int =
5
...
By CHI, these are equivalent to the preferred style
1
let
f (a : int ) (b: int ) : int =
2
...
We call this currying, and say that
curried
.9. A minor tangent
We have
gcd : int * int -> intgcd : int -> (int -> int)
Through currying and uncurrying, these types are somehow ”equiv-alent” (there exists some isomorph between these that preservesbehavior)10. Polymorphism2

1
let
id x = x
This will have type
α
α
, or in caml parlance
1
a
>
a
11. Polymorphic types:
1
type
’a lst =
2
Empty
3
|
Cons
o
( ’a
’a lst )
also recursiv
)
12.
OCaml
lists builtin
[] is the empty list
:: is the cons operator
@ is the append operator
[1;2;3]
1 :: 2 :: 3 :: []13. @ is not constant time, it’s a bad idea.
1
let rec
reverse ( l : ’a l i s t ) : ’a l i s t =
2
match
l
with
3
[ ]
>
[ ]
4
|
hd : : tl
>
( reverse tl ) @ [hd]
14. Instead, we should write something more idiomatic like
1
let rec
reverse ( l : ’a l i s t ) : ’a l i s t =
2
let rec
reverse ( l ’ : ’a l i s t ) ( l : ’a l i s t ) : ’a l i s t =
3
match
l
with
4
[ ]
>
l
5
|
hd : : tl
>
reverse tl (hd : : l )
in
6
reverse l [ ]