DOC PREVIEW
CALTECH CS 11 - Ocaml track

This preview shows page 1-2-3-21-22-23-43-44-45 out of 45 pages.

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

Unformatted text preview:

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 1PreliminariesNeed a CS cluster accounthttp://www.cs.caltech.edu/cgi-bin/sysadmin/account_request.cgiNeed to know UNIXITS tutorial linked from track home pageTrack home page:www.cs.caltech.edu/courses/cs11/material/ocamlAssignments1st assignment is posted nowDue one week after class, midnightLate penalty: 1 mark/dayRedosRedos1st redo = 1 mark of2nd redo = 1 to 2 more marks of3rd redo = 1 to 3 more marks ofNo 4th redo! Grade - 6 mark penaltyPassingNeed average of 7/10 on labs6 labs  42/60 marksOther administrative stufSee admin web page:http://www.cs.caltech.edu/courses/cs11/material/ocaml/admin.htmlCovers how to submit labs, collaboration policy, grading, etc.TextbookIntroduction to Objective Caml by Jason Hickeydraft (don't redistribute)Ocaml: prosOcaml is a very nice language!Strong static type systemcatches lots of errors at compile timeVery expressive type systemfirst-class functionspolymorphic typesalgebraic data typesmakes it easy to build complex data typesreferences for mutable dataOcaml: prosGarbage collectionByte-code and native-code compilersVery fast!very competitive with C and C++especially if data structures are very complexInteractive interpreter for experimentingClean designCan interface with C fairly easilyOcaml: prosFully supports several diferent programming paradigms:functional programmingimperative programmingobject-oriented programmingMost natural to use as a "mostly-functional" languageSafe language: no core dumps!But wait! There's more!Type inference to get benefits of static typing without having to write out tons of declarationsVery powerful module systemincluding separate compilation of modulesParameterizable modules (functors)Simple and powerful exception handling systemPlus more experimental featuresOcaml: consVery few bad things about ocamlNative-code compiler doesn't support shared librariesthough 3rd-party tools can do thisType system sometimes too rigidObject system doesn't support "downcasting" i.e. "instanceof"Ocaml: consMessy, ambiguous syntax"Operator underloading"+ to add integers+. to add floatsFor purists: not as purely functional as e. g. HaskellSome messy aspects of type system"polymorphic references"Ocaml: usesGreat language for writing compilers!Also great for writing theorem proversRecently, Ocaml used for tasks in many other areas:simulationsfinanceoperating systemsetc.Ocaml: usesCan compete successfully with C/C++Especially whensafety is importantdata structures are very complexIn these cases, can often outperform C/C++Example: Ensemble system re-written from C  Ocaml; new version fasterOcaml: usesWhy 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++ programmersand still have correct, working codeOcaml: historyOcaml is a dialect of the "ML" languageML originally the "meta-language" for a theorem-proving program called "LCF""Logic for Computable Functions"Ocaml: historyAdapted 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 extensionsPrime candidate for worst computer language name of all timeOur emphasisIn this track, we will focus on Ocaml's use as a functional programming languageWe will also cover imperative aspectsbut not OO featuresGood preparation for e.g. CS 134b (compiler course)Functional programmingWhat is a functional programming language?It's a language thattreats functions as "first-class" dataMeaning?Functions can bepassed as argumentscreated on-the-flyreturned as a result from other functionsFunctional programmingOther aspects of FP:Data should be persistentnames, 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 listsAssignment statements rarely usedExplicit loops rarely used; use recursion insteadHigher-order functions used a lotFunctional programmingLearning the syntax of Ocaml is relatively easyLearning to program in a "functional style" is much harderMain 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 startedThe interactive interpreter is just called ocamlGet out of it by typing control-D (^D AKA end-of-file)When inside, can do essentially anything that could be done in a filedefine functionsdefine typesrun codeGetting startedThe "hello, world!" program (sort of):% ocaml Objective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%Getting startedThe "hello, world!" program (sort of):% ocaml Objective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%promptGetting startedThe "hello, world!" program (sort of):% ocaml Objective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%statementGetting startedThe "hello, world!" program (sort of):% ocaml Objective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%side efectGetting startedThe "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 startedIn interactive


View Full Document

CALTECH CS 11 - Ocaml track

Download Ocaml track
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 Ocaml track 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 Ocaml track 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?