Tải bản đầy đủ (.pdf) (8 trang)

OBJECT CAML FIRST ENCOUNTERS LOADING FROM FILES RECURSION IN OCAML TWO AT A BLOW

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (206.05 KB, 8 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

<i>Ocaml programming languageAn introduction</i>

Theory of Programming LanguagesComputer Science Department

Wellesley College

<small>Object CamlFirst EncountersLoading from FilesRecursion in OcamlTwo at a Blow</small>

<i>Table of contents</i>

<i>Object CamlFirst EncountersLoading from FilesRecursion in OcamlTwo at a Blow</i>

</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">

<i>Object Caml</i>

<i>•</i>Ocaml is a dialect of ML(Meta-Language) designedby Robin Milner in 1975 forspecifying theorem provers.

<i>•</i>ML is a<i>strongly typed,functional language</i>thatuses<i>type inference</i>to infertypes.

<i>•</i>Unlike Jave, the ML typesystem is<i>polymorphic</i>.

<small>Object CamlFirst EncountersLoading from FilesRecursion in OcamlTwo at a Blow</small>

for a side-by-side comparison of SML and Ocaml. (This is

<i>linked from the CS251 Resources page.)</i>

<i>•</i>Best way to learn is to fire up emacs in Ocaml mode. See“Using Ocaml” for details.

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

<i>Ocaml in action</i>

# 1 + 2;;

# 1 + 2;;

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

<i>Still more Ocaml</i>

# let f n =

if n > 10 then2 * n

elsen * n;;# f 20;;

# f 5;;

# let rec fact n =if n = 0 then

n * (fact2 (n-1));;

<small>Object CamlFirst EncountersLoading from FilesRecursion in OcamlTwo at a Blow</small>

<i>Enough with the Ocaml already</i>

# 1 = 1;;# 2 = 3;;# "foobar";;

# String.length "foobar";;

# String.get "foobar" 5;;

# "baz" ^ "quux" ^ (string_of_int 17);;

# (2 * 3, 4 < 5, "foo" ^ "bar", String.get "baz" 2);;

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

<i>Swapping and stepping</i>

# let swap (a,b) = (b,a);;

# let (x,y) = step (step (1,2)) in x+y;;

<small>Object CamlFirst EncountersLoading from FilesRecursion in OcamlTwo at a Blow</small>

<i>Dealing with longer functions</i>

<i>•</i>It is tedious to type alldeclarations directly to theOcaml interpreter,

especially if your code isbuggy and needs be retyped.

<i>#use "filename "</i>

evaluates all of expressionsin the file, as if you hadtyped them in by hand.

<i>•</i>The #use directive can beused within a file to loadother files.

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

<small>let adda x = x + a;;let a = 42;;let f n = if n > 10</small>

<small>then 2 * nelse n * n;;let rec fact n =</small>

<small>if n = 0 then1</small>

<small>n * (fact (n-1));;let swap (a,b) = (b,a);;let step (a,b) = (a + b, a*b);;</small>

<small>Object CamlFirst EncountersLoading from FilesRecursion in OcamlTwo at a Blow</small>

<i>Close encounters of the third kind</i>

We load FunTest.ml into the interpreter using #use directive:

# #use "FunTest.ml";;val a : int = 7

val dbl : int -> int = <fun>val inc : int -> int = <fun>

val app5 : (int -> ’a) -> ’a = <fun>val b : int = 14

val adda : int -> int = <fun>val a : int = 42

val f : int -> int = <fun>val fact : int -> int = <fun>

val swap : ’a * ’b -> ’b * ’a = <fun>val step : int * int -> int * int = <fun>

val stepuntil : (int * int) * int -> int * int = <fun>val print_pair : int * int -> unit = <fun>

val stepuntil : (int * int) * int -> int * int = <fun>val sumDivisors : int -> int = <fun>

val numDivisors : int -> int = <fun>

val numAndSumDivisors : int -> int * int = <fun>val avg1 : int * int -> int = <fun>

val avg2 : int -> int -> int = <fun>

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

<i>Of particular interest in FunTest.ml</i>

# let rec stepuntil ((a,b),limit) =if a >= limit then

stepuntil(step(a,b),limit);;# stepuntil ((1,2), 100);;

# let print_pair (a,b) =

print_string ("(" ^ (string_of_int a) ^ ","^ (string_of_int b) ^ ")\n");;# let rec stepuntil ((a,b),limit) =

if a >= limit then(a,b)

(print_pair (a,b);

stepuntil(step(a,b),limit));;# stepuntil ((1,2),100);;

<small>Object CamlFirst EncountersLoading from FilesRecursion in OcamlTwo at a Blow</small>

# let sumDivisors n =if n <= 0 then

let rec sum d =if d == 0 then

# sumDivisors 12;;

# let numDivisors n =if n <= 0 then

let rec sum d =if d == 0 then

# numDivisors 12;;

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

<i>Recursion with pairs</i>

# let numAndSumDivisors n =if n <= 0 then

let rec sum d =if d == 0 then

else if (n mod d) == 0 thenlet (n,s) = sum (d-1)

in (1+n,d+s)else

sum (d-1)in sum (n-1);;

</div>

×