CS 11 Ocaml track: lecture 1AssignmentsRedosPassingOther administrative stuffTextbookOcaml: prosOcaml: prosOcaml: prosBut wait! There's more!Ocaml: consOcaml: consOcaml: usesOcaml: usesOcaml: usesOcaml: historyOcaml: historyOur emphasisFunctional programmingFunctional programmingFunctional programmingGetting startedGetting startedGetting startedGetting startedGetting startedGetting startedGetting startedStand-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 off 2nd redo = 1 to 2 more marks off 3rd redo = 1 to 3 more marks off No 4th redo! Grade - 6 mark penaltyPassing Need average of 7/10 on labs 6 labs Æ 42/60 marksOther administrative stuff 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 Camlby 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 different 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 different 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):% ocamlObjective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%Getting started The "hello, world!" program (sort of):% ocamlObjective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%promptGetting started The "hello, world!" program (sort of):% ocamlObjective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%statementGetting started The "hello, world!" program (sort of):% ocamlObjective Caml version 3.08.3# Printf.printf "hello, world!\n";;hello, world!- : unit = ()^D%side effectGetting started The "hello, world!"
View Full Document