DOC PREVIEW
CALTECH CS 11 - Lecture notes

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

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

Unformatted text preview:

CS 11 Ocaml track: lecture 3Equality/inequality operatorsUnit typeOption typesString accessing/mutating (1)String accessing/mutating (2)printf and friends (1)printf and friends (2)printf and friends (3)printf and friends (4)File I/O (1)File I/O (2)File I/O (3)File I/O (4)begin/end and sequencing (1)begin/end and sequencing (2)begin/end and sequencing (3)begin/end and sequencing (4)assertOn to...Imperative programmingReferences (1)References (2)while loopExampleRecords with mutable fields (1)Records with mutable fields (2)Records with mutable fields (3)Records with mutable fields (4)Arraysfor loopsBreaking out of loopsNext timeCS 11 Ocaml track: lecture 3Today:A (large) variety of odds and endsImperative programming in ocamlEquality/inequality operatorsTwo inequality operators: <> and !=Two equality operators: = and ==Usually want to use = and <>= means "structurally equal"<> means "structurally unequal"== means "the same exact object"!= means "not the same exact object"Unit typeThe unit type is a type with only one member: ()not a tuple with only one element!tuples must have at least two elementsSeems useless, butall ocaml functions must return a valuereturn () when value is irrelevanti.e. when function called for side effectsOption typestype 'a option = | None | Some of 'aBuilt in to ocamlUsed for functions that can return a value but can also "fail" (return None)Alternative to raising exception on failureString accessing/mutating (1)Strings are not immutableCan treat as an array of charsTo access a particular char:s.[i]To mutate a particular char:s.[i] <- 'a'String accessing/mutating (2)# let s = "some string" ;;val s : string = "some string"# s.[0] ;; (* note weird syntax *)- : char = 's'# s.[0] <- 't' ;;- : unit = ()# s ;;- : string = "tome string"printf and friends (1)# Printf.printf "hello, world!\n" ;;hello, world!- : unit = ()# open Printf ;;# printf "hello, world!\n" ;;hello, world!- : unit = ()printf and friends (2)# printf "s = %s\tf = %f\ti = %d\n" "foo" 3.2 1 ;;s = foo f = 3.200000 i = 1- : unit = ()printf has a weird typenot really well-typedcompiler "knows" about it and makes it workprintf and friends (3)# fprintf stderr "Oops! An error occurred!\n" ;;- : unit = ()# stderr ;;- : out_channel = <abstr>Predefined I/O "channels":stdin : in_channelstdout : out_channelstderr : out_channelprintf and friends (4)# sprintf "%d + %d = %d\n" 2 2 4 ;;- : string = "2 + 2 = 4\n"sprintf is "printing to a string"Very useful!File I/O (1)Files come in two flavors: input and output# open_in ;;- : string -> in_channel = <fun># open_out ;;- : string -> out_channel = <fun># close_in ;;- : in_channel -> unit = <fun># close_out ;;- : out_channel -> unit = <fun>File I/O (2)Files come in two flavors: input and outputlet infile = open_in "foo"tries to open file named "foo" for input onlybinds file object to infileclose_in infilecloses the input fileFile I/O (3)Files come in two flavors: input and outputlet outfile = open_out "bar"tries to open file named "bar" for output onlybinds file object to outfileclose_out outfilecloses the output fileFile I/O (4)flush stdoutforces an output file (here, stdout) to write its buffers to the diskinput_line stdingets a line of input from an input file (here, stdin) and returns a stringbegin/end and sequencing (1)With side effects, often want multiple statements inside a function:let print_and_square x = Printf.printf "%d\n" x ; x * xSingle semicolon used to separate statements that execute one after anotherbegin/end and sequencing (2)Sometimes want to say "these sequences should be treated as a single expression"Use begin/end for this:begin Printf.printf "%d\n" x; x * xendCan often leave out begin/endbegin/end and sequencing (3)Sometimes can just use parentheses:(Printf.printf "%d\n" x ; x * x)I advise against thisCan make code hard to readbegin/end and sequencing (4)Very often, when you get weird error messages it's because you should have put in a begin/end somewhereCommonly found in nested match expressions (ocaml grammar is highly ambiguous!)When in doubt, add explicit begin/end statements everywhere you use sequencingassertOcaml has an assert statement like most imperative languagesNot a function!Takes one "argument", a booleanIf it's false, raises Assert_failure exceptionTurn off assertions with -noassert compiler optionOn to...Imperative programming!We've already done imperative programmingprintf is a function called for side-effects onlybegin/end and sequencing only useful for side effecting operationsNow want to cover the "core" of imperative programmingImperative programmingImperative data types:referencesrecords with mutable fieldsmutable arraysImperative statements:for loopwhile loopBreaking out of loopsReferences (1)A reference type is like a regular type in C/C++/Java/Python/whateverCan also think of as a box that holds a single value# let x = ref 0 ;;val x : int ref = {contents = 0}# !x ;;- : int = 0References (2)The ! operator fetches the value from the reference "box"The := operator assigns a new value to the reference# x := 10 ;;- : unit = ()# x ;;- : int ref = {contents = 10}LHS of := must be a reference, not a value!while loopwhile loop is basically like C/C++/Java while loop:while <condition> do <stmt1>; <stmt2>; ... <stmtn>doneExamplelet factorial n = let result = ref 1 in let i = ref n in while !i > 1 do result := !result * !i; i := !i - 1 done; !resultVery easy to accidentally omit ! operatorsRecords with mutable fields (1)References are just a special case of records with mutable fieldsRecall record type declaration:type point = { x: int; y: int }This declares point as an i mmutable typex and y fields can't change after point creatednot always what you wantRecords with mutable fields (2)To get mutable fields:type point = { mutable x: int; mutable y: int }Now can change x, y fields:let p = { x = 10; y = 20 } ;;val p : point = {x = 10; y = 20}# p.x <- 1000 ;;- : unit = ()# p ;;- : point = {x = 1000; y = 20}Records with mutable fields (3)To get only some mutable fields:type point = { x: int; mutable y: int }Now can change only change y field:# let p = { x = 10; y = 20 } ;;val p : point =


View Full Document

CALTECH CS 11 - Lecture notes

Download Lecture notes
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 Lecture notes 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 Lecture notes 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?