lecture-4

lecture-4 - OCaml The PL for the discerning hacker. Hello....

Info iconThis preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon
OCaml The PL for the discerning hacker.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu
Background image of page 2
ML Anatomy 101 ML Program = One Giant, Complex Expression Controlling complexity is the essence of computer programming. B. Kerninghan A complex system that works is invariably found to have evolved from a simple system that worked. J. Gall ML Program = ? ? ?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Building ML Programs ML provides tools to control complexity Build complex exprs from simple exprs Build complex types from simple types NOW THU
Background image of page 4
Building Expressions 1. basic (recap) 2. let 3. if 4. fun 5. demo M.C. Escher’s Waterfall in LEGO
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
basic Type Expression Value int 5 5 int 5 * 5 * 5 125 string “hello” “hello” string “Je” ^ “ ll ” ^ “o” Jello tuple (5 * 5, “ ab ” ^ “ cd ”) (25, “ abcd ”) int list [1; 2] @ [3; 4] [1; 2; 3; 4] int list 1 :: 2 :: 3 :: 4 :: [] [1; 2; 3; 4]
Background image of page 6
basic Don’t know how it works ? Try it in the toplevel !
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Building Expressions 1. basic (recap) 2. let 3. if 4. fun 5. demo M.C. Escher’s Waterfall in LEGO
Background image of page 8
Variables are central to programming Associate a name with a computation let expressions are how ML does it let let
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
let Bind name NM to expression E1 within E2 : let NM = E1 in E2 Semantics (what it means): 1. evaluate E1 to value V 2. replace NM with V in E2
Background image of page 10
let examples let x = 5 in x let x = 5 in x * x let x = 5 * 5 in x * x let x = “hello” in print_string x let print = print_string in print “hello”
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
let chaining (outside) Let syntax : let NM = E1 in E2 E2 can be another let let x = 2 in let y = 3 in let x2 = x * x in let y2 = y * y in x2 + y2
Background image of page 12
let nesting (inside) Let syntax : let NM = E1 in E2 E1 can be another let let x2 = let x = 5 in x * x in x2 + x2
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
let name clashes (outside) Let syntax : let NM = E1 in E2 What if NM appears in E2 ? let x = 1 in let x = 2 in x Our naïve semantics were wrong!
Background image of page 14
let name clashes Let syntax : let NM = E1 in E2 Semantics (what it means): 1. evaluate E1 to value V 2. replace UNBOUND NM with V in E2 Essentially, use nearest binding.
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Let syntax : let NM = E1 in E2 What if NM appears in E1 ? let x =
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 80

lecture-4 - OCaml The PL for the discerning hacker. Hello....

This preview shows document pages 1 - 17. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online