DOC PREVIEW
UMD CMSC 330 - Functional Programming with OCaml

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 330: Organization of Programming LanguagesFunctional Programming with OCaml2Background• ML (Meta Language)– Univ. of Edinburgh, 1973– Part of a theorem proving system LCF• The Logic of Computable Functions• SML/NJ (Standard ML of New Jersey)– Bell Labs and Princeton, 1990– Now Yale, AT&T Research, Univ. of Chicago (among others)• OCaml (Objective CAML)– INRIA, 1996– French Nat’lInstitute for Research in Computer Science3Dialects of ML• Other dialects include MoscowML, ML Kit, Concurrent ML, etc.– But SML/NJ and OCaml are most popular– O = “Objective,” but probably won’t cover objects• Languages all have the same core ideas– But small and annoying syntactic differences– So you should not buy a book with ML in the title• Because it probably won’t cover OCaml4More Information on OCaml• Translation available on the class webpage– Developing Applications with Objective Caml• Webpage also has link to another book– Introduction to the Objective Caml Programming Language5Features of ML• Higher-order functions– Functions can be parameters and return values• “Mostly functional”• Data types and pattern matching– Convenient for certain kinds of data structures• Type inference– No need to write types in the source language• But the language is statically typed– Supports parametric polymorphism• Generics in Java, templates in C++• Exceptions• Garbage collection6Functional languages• In a pure functional language, every program is just an expression evaluationlet add1 x = x + 1;;let rec add (x,y) = if x=0 then y else add(x-1, add1(y));;add(2,3) = add(1,add1(3)) = add(0,add1(add1(3)))= add1(add1(3)) = add1(3+1) = 3+1+1= 5OCaml has this basic behavior, but has additional features to ease the programming process.- Less emphasis on data storage- More emphasis on function execution7A Small OCaml Program- Things to Notice(* A small OCaml program *)let x = 37;;let y = x + 5;;print_int y;;print_string"\n";;Use (* *) for comments (may nest);; ends a top-level expressionUse let to bind variablesNo type declarationsNeed to use correct print function(OCaml also has printf)Line breaks, spacing ignored (like C, C++, Java, not like Ruby)8Run, OCaml, Run• OCaml programs can be compiled using ocamlc– Produces .cmo (“compiled object”) and .cmi(“compiled interface”) files• We’ll talk about interface files later– By default, also links to produce executable a.out• Use -o to set output file name• Use -c to compile only to .cmo/.cmi and not to link• You'll be given a Makefile if you need to compile your files9Run, OCaml, Run (cont’d)• Compiling and running the previous small program:% ocamlc ocaml1.ml% ./a.out42%(* A small OCaml program *)let x = 37;;let y = x + 5;;print_int y;;print_string "\n";;ocaml1.ml:10Run, OCaml, Run (cont’d)Expressions can also be typed and evaluated at the top-level:# 3 + 4;;- : int = 7# let x = 37;;val x : int = 37# x;;- : int = 37# let y = 5;;val y : int = 5# let z = 5 + x;;val z : int = 42# print_int z;;42- : unit = ()# print_string "Colorless green ideas sleep furiously";;Colorless green ideas sleep furiously- : unit = ()# print_int "Colorless green ideas sleep furiously";;This expression has type string but is here used with type intgives type and value of each exprunit = “no interesting value” (like void)“-” = “the expression you just typed”11Run, OCaml, Run (cont’d)• Files can be loaded at the top-level% ocamlObjective Caml version 3.08.3# #use "ocaml1.ml";;val x : int = 37val y : int = 4242- : unit = ()- : unit = ()# x;;- : int = 37#use loads in a file one line at a time(* A small OCaml program *)let x = 37;;let y = x + 5;;print_int y;;print_string "\n";;ocaml1.ml:12Basic Types in OCaml• Read e : t has “expression e has type t”42 : int true : bool"hello" : string 'c' : char3.14 : float () : unit (* don’t care value *)• OCaml has static types to help you avoid errors– Note: Sometimes the messages are a bit confusing# 1 + true;;This expression has type bool but is here used with type int– Watch for the underline as a hint to what went wrong– But not always reliable13More on the Let Construct• let is more often used for local variables– let x = e1 in e2 means• Evaluate e1• Then evaluate e2, with x bound to result of evaluating e1• x is not visible outside of e2let pi = 3.14 in pi *. 3.0 *. 3.0;;pi;;bind pi in body of let floating point multiplicationerror14More on the Let Construct (cont’d)• Compare to similar usage in Java/C• In the top-level, omitting in means “from now on”:# let pi = 3.14;;(* pi is now bound in the rest of the top-level scope *)let pi = 3.14 inpi *. 3.0 *. 3.0;;pi;;{float pi = 3.14;pi * 3.0 * 3.0;}pi;15Nested Let• Uses of let can be nestedlet pi = 3.14 inlet r = 3.0 inpi *. r *. r;;(* pi, r no longer in scope *){float pi = 3.14;float r = 3.0;pi * r * r;}/* pi, r not in scope */16Defining Functionslet next x = x + 1;;next 3;;let plus (x, y) = x + y;;plus (3, 4);;use let to define functionslist parameters after function nameno return statementno parentheses on function calls17Local Variables• You can use let inside of functions for locals– And you can use as many lets as you wantlet area r =let pi = 3.14 inpi *. r *. rlet area d =let pi = 3.14 inlet r = d /. 2.0 inpi *. r *. r18Function Types• In OCaml, -> is the function type constructor– The type t1 -> t2 is a function with argument or domain type t1 and return or range type t2• Examples– let next x = x + 1 (* type int -> int *)– let fn x = (float_of_int x) *. 3.14(* type int -> float *)– print_string (* type string -> unit *)• Type a function name at top level to get its type19Type Annotations• The syntax (e : t) asserts that “e has type t”– This can be added anywhere you likelet (x : int) = 3let z = (x : int) + 5• Use to give functions parameter and return typeslet fn (x:int):float =(float_of_int x) *. 3.14– Note special position for return type– Thus let g x:int =... means g returns int• Very useful for debugging, especially for more complicated types20;; versus ;• ;; ends an expression in the top-level of OCaml– Use it to say: “Give me the value of this expression”– Not used in the body of a function– Not needed after each function definition• Though for now it won’t hurt if used there• e1; e2 evaluates e1 and then e2, and returns e2let print_both (s, t) = print_string s; print_string


View Full Document

UMD CMSC 330 - Functional Programming with OCaml

Documents in this Course
Exam #1

Exam #1

6 pages

Quiz #1

Quiz #1

2 pages

Midterm 2

Midterm 2

12 pages

Exam #2

Exam #2

7 pages

Ocaml

Ocaml

7 pages

Parsing

Parsing

38 pages

Threads

Threads

12 pages

Ruby

Ruby

7 pages

Quiz #3

Quiz #3

2 pages

Threads

Threads

7 pages

Quiz #4

Quiz #4

2 pages

Exam #2

Exam #2

6 pages

Exam #1

Exam #1

6 pages

Threads

Threads

34 pages

Quiz #4

Quiz #4

2 pages

Threads

Threads

26 pages

Exam #2

Exam #2

9 pages

Exam #2

Exam #2

6 pages

Load more
Download Functional Programming with OCaml
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functional Programming with OCaml and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functional Programming with OCaml 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?