CS 11 Ocaml track: lecture 1AssignmentsRedosPassingOther administrative stuffTextbookOcaml: prosSlide 8Slide 9But wait! There's more!Ocaml: consSlide 12Ocaml: usesSlide 14Slide 15Ocaml: historySlide 17Our emphasisFunctional programmingSlide 20Slide 21Getting startedSlide 23Slide 24Slide 25Slide 26Slide 27Slide 28Stand-alone executables (1)Stand-alone executables (2)All right, then...Basic data types (1)Basic data types (2)Operatorslet expressionsDefining functions (1)Defining functions (2)Pattern matching (1)Pattern matching (2)Higher-order functions (1)Higher-order functions (2)Pattern guardsTail recursion (1)Tail recursion (2)That's all for nowCS 11 Ocaml track: lecture 1PreliminariesNeed a CS cluster accounthttp://www.cs.caltech.edu/cgi-bin/sysadmin/account_request.cgiNeed to know UNIXITS tutorial linked from track home pageTrack home page:www.cs.caltech.edu/courses/cs11/material/ocamlAssignments1st assignment is posted nowDue one week after class, midnightLate penalty: 1 mark/dayRedosRedos1st redo = 1 mark of2nd redo = 1 to 2 more marks of3rd redo = 1 to 3 more marks ofNo 4th redo! Grade - 6 mark penaltyPassingNeed average of 7/10 on labs6 labs 42/60 marksOther administrative stufSee admin web page:http://www.cs.caltech.edu/courses/cs11/material/ocaml/admin.htmlCovers how to submit labs, collaboration policy, grading, etc.TextbookIntroduction to Objective Caml by Jason Hickeydraft (don't redistribute)Ocaml: prosOcaml is a very nice language!Strong static type systemcatches lots of errors at compile timeVery expressive type systemfirst-class functionspolymorphic typesalgebraic data typesmakes it easy to build complex data typesreferences for mutable dataOcaml: prosGarbage collectionByte-code and native-code compilersVery fast!very competitive with C and C++especially if data structures are very complexInteractive interpreter for experimentingClean designCan interface with C fairly easilyOcaml: prosFully supports several diferent programming paradigms:functional programmingimperative programmingobject-oriented programmingMost natural to use as a "mostly-functional" languageSafe language: no core dumps!But wait! There's more!Type inference to get benefits of static typing without having to write out tons of declarationsVery powerful module systemincluding separate compilation of modulesParameterizable modules (functors)Simple and powerful exception handling systemPlus more experimental featuresOcaml: consVery few bad things about ocamlNative-code compiler doesn't support shared librariesthough 3rd-party tools can do thisType system sometimes too rigidObject system doesn't support "downcasting" i.e. "instanceof"Ocaml: consMessy, ambiguous syntax"Operator underloading"+ to add integers+. to add floatsFor purists: not as purely functional as e. g. HaskellSome messy aspects of type system"polymorphic references"Ocaml: usesGreat language for writing compilers!Also great for writing theorem proversRecently, Ocaml used for tasks in many other areas:simulationsfinanceoperating systemsetc.Ocaml: usesCan compete successfully with C/C++Especially whensafety is importantdata structures are very complexIn these cases, can often outperform C/C++Example: Ensemble system re-written from C Ocaml; new version fasterOcaml: usesWhy should Ocaml give faster code in those cases?After all, C/C++ "closer to the machine"Answer:easier to tweak very complex algorithms in ways that would overwhelm C/C++ programmersand still have correct, working codeOcaml: historyOcaml is a dialect of the "ML" languageML originally the "meta-language" for a theorem-proving program called "LCF""Logic for Computable Functions"Ocaml: historyAdapted into a language called CAML by researchers in INRIA (France)"Categorical Abstract Machine Language"Newer versions have a very diferent internal structure, but kept name"Ocaml" is "Objective Caml"CAML with object-oriented extensionsPrime candidate for worst computer language name of all timeOur emphasisIn this track, we will focus on Ocaml's use as a functional programming languageWe will also cover imperative aspectsbut not OO featuresGood preparation for e.g. CS 134b (compiler course)Functional programmingWhat is a functional programming language?It's a language thattreats functions as "first-class" dataMeaning?Functions can bepassed as argumentscreated on-the-flyreturned as a result from other functionsFunctional programmingOther aspects of FP:Data should be persistentnames, once bound, do not get rebound(unless they are function arguments)mutable data structures like arrays avoided in favor of non-mutable data structures like singly-linked listsAssignment statements rarely usedExplicit loops rarely used; use recursion insteadHigher-order functions used a lotFunctional programmingLearning the syntax of Ocaml is relatively easyLearning to program in a "functional style" is much harderMain goal of track is to force you to learn to think this way(If you've taken CS 1, you already know how to think this way)Getting startedThe interactive interpreter is just called ocamlGet out of it by typing control-D (^D AKA end-of-file)When inside, can do essentially anything that could be done in a filedefine functionsdefine typesrun codeGetting startedThe "hello, world!" program (sort of):% ocaml Objective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%Getting startedThe "hello, world!" program (sort of):% ocaml Objective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%promptGetting startedThe "hello, world!" program (sort of):% ocaml Objective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%statementGetting startedThe "hello, world!" program (sort of):% ocaml Objective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%side efectGetting startedThe "hello, world!" program (sort of):% ocaml Objective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%result name, type and valueGetting startedIn interactive
View Full Document