DOC PREVIEW
UMD CMSC 330 - Functional Programming with OCaml

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 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 30 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 30 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 30 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 30 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 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 330: Organization ofProgramming LanguagesFunctional Programming with OCamlCMSC 330 2Background• 1973 – ML developed at Univ. of Edinburgh– Part of a theorem proving system LCF• The Logic of Computable Functions• SML/NJ (“Standard ML of New Jersey”)– http://www.smlnj.org– Developed at Bell Labs and Princeton; now Yale,AT&T Research, Univ. of Chicago (among others)• OCaml– http://www.ocaml.org– Developed at INRIA (The French National Institutefor Research in Computer Science)CMSC 330 3Dialects 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 OCamlCMSC 330 4More Information on OCaml• Translation availableon the class webpage– DevelopingApplications withObjective Caml• Webpage also has linkto another book– Introduction to theObjective CamlProgrammingLanguageCMSC 330 5Features 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 languageis statically typed– Supports parametric polymorphism (generics in Java, templatesin C++)• Exceptions• Garbage collectionCMSC 330 6Functional languages• In a pure functional language, every program isjust 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 additionalfeatures to ease the programming process.- Less emphasis on data storage- More emphasis on function executionCMSC 330 7A 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 (likeC, C++, Java, not like Ruby)CMSC 330 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 filesCMSC 330 9Run, OCaml, Run (cont’d)• Compiling and running the previous smallprogram:% ocamlc ocaml1.ml% ./a.out42%(* A small OCaml program *)let x = 37;;let y = x + 5;;print_int y;;print_string "\n";;ocaml1.ml:CMSC 330 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”CMSC 330 11Run, OCaml, Run (cont’d)• Files can be loaded at the top-level % ocaml Objective 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:CMSC 330 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 withtype int– Watch for the underline as a hint to what went wrong– But not always reliableCMSC 330 13More 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 multiplicationerrorCMSC 330 14More 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 in pi *. 3.0 *. 3.0;;pi;;{ float pi = 3.14; pi * 3.0 * 3.0;}pi;CMSC 330 15Nested Let• Uses of let can be nestedlet pi = 3.14 inlet r = 3.0 in pi *. r *. r;;(* pi, r no longer in scope *){ float pi = 3.14; float r = 3.0; pi * r * r;}/* pi, r not in scope */CMSC 330 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 callsCMSC 330 17Local 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 in pi *. r *. rlet area d = let pi = 3.14 in let r = d /. 2.0 in pi *. r *. rCMSC 330 18Function Types• In OCaml, -> is the function type constructor– The type t1 -> t2 is a function with argument ordomain 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 typeCMSC 330 19Type Annotations• The syntax (e : t) asserts that “e has type t”– This can be added anywhere you like let (x : int) = 3 let z = (x : int) + 5• Use to give functions parameter and return types let 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 morecomplicated typesCMSC 330 20;; versus ;• ;; ends an expression in the top-level of OCaml– Use it to say: “Give me the value


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?