Commit d2ac348f authored by James R. Wilcox's avatar James R. Wilcox
Browse files

L07 materials

parent 092efdce
; doesn't work because x is not bound yet
; (define y (+ x 1))
; dynamic environment: empty
(define x 5)
; dynamic environment: x -> 5
(define y (+ x 1))
; dynamic environment: x -> 5, y -> 6
(* x y)
; dynamic environment: x -> 5, y -> 6
(define z 7)
; dynamic environment: x -> 5, y -> 6, z -> 7
(+ z 1)
(+ z x)
; fails at run time
; (+ true 1)
; phases and errors
; - tokenization
; - PST parsing
; - AST parsing
; - (Ocaml only; not trefoil) type checking
; ------------------- compile time ^ ---- run time v --------------------
; - interpretation (runtime error)
; (+ x 1 ; PST error: missing paren
; (+ 1 2 3) ; AST error: too many args to +
; (+ true 1) ; runtime error
; transport into OCaml for one sec
;
; (* dynenv: empty *)
; let x = 3 (* top level Ocaml variable binding *)
; (* dynenv: x -> 3 *)
;
; let w =
; let z = x + 1 in (* local variable binding *)
; (* temporary dynenv: x -> 3, z -> 4 *)
; x * z
; (* body of the let ends; throw away temp environment *)
;
; (* dynenv: x -> 3, w -> 12 *)
;
; let a = ... (* z is not available *)
; dynamic environment: x -> 5, y -> 6, z -> 7
(define x 3)
; dynamic environment: x -> 5, y -> 6, z -> 7, x -> 3
(define w
(let ((z (+ x 1)))
; temp dynamic environment: x -> 5, y -> 6, z -> 7, x -> 3, z -> 4
(* x z)))
; 12
; throw away temp environment
; dynamic environment: x -> 5, y -> 6, z -> 7, x -> 3, w -> 12
(define a z)
(define b (+ z w))
;;;;;;;;;;;;;;;;;;;;;;;;;
; Trefoil pairs and lists
;;;;;;;;;;;;;;;;;;;;;;;;;
; constructs a pair of 0 and 1, like (0, 1) in OCaml
(define p (cons 0 1))
; nested pairs
(cons (cons 0 1) (cons 2 3))
; empty list
nil
(define l (cons 1 (cons 2 (cons 3 nil)))) ; like [1; 2; 3] in OCaml
; "improper" lists (don't end in nil)
(cons 1 (cons 2 (cons 3 4))) ; type error in OCaml
;; using pairs and lists
(nil? l) ; check if a list is nil (empty)
(nil? nil)
(car p) ;; like fst in OCaml
(cdr p) ;; like snd in OCaml; pronounced "could-er"
(car l) ;; like List.hd in OCaml
(cdr l) ;; like List.tl in OCaml
(car (cdr l))
(cdr (cdr l))
(car (cdr (cdr l)))
(cdr (cdr (cdr l)))
;;;;;;;;;;;;;;
; Functions
;;;;;;;;;;;;;;
(define (increment x) ; a new kind of top-level binding: function bindings
(+ x 1))
(increment 3) ; function call
; (increment) ;; runtime error: wrong number of arguments
(define (look-ma-no-args)
7)
(look-ma-no-args)
; look-ma-no-args ;; runtime error: referring to function name as a variable
(define look-ma-a-var
7)
look-ma-a-var
; (look-ma-a-var) ;; runtime error: referring to variable as a function
(define (pow base exp)
(if (= exp 0)
1
(* base (pow base (- exp 1)))))
(pow 2 3)
(define (countdown n)
(if (= n 0)
nil
(cons n (countdown (- n 1)))))
(countdown 3)
(define (sum l)
(if (nil? l)
0
(+ (car l) (sum (cdr l)))))
(sum (countdown 10))
\ No newline at end of file
(* records *)
(* records have the same "expressive power" as tuples, just with
* user-defined field names and different syntax for building and using
* but our first time making our own new type (!)
*)
type lava_lamp =
{ height : float
; color_liquid : string
; color_lava : string
}
let my_lamp1 =
{ height = 13.5 +. 1.0
; color_liquid = "bl" ^ "ue"
; color_lava = "" ^ "green" ^ ""
}
let my_lamp2 = { height = 14.4; color_liquid = my_lamp1.color_liquid; color_lava = "x" }
let a = my_lamp1.height
let b = my_lamp1.color_liquid
let c = my_lamp1.color_lava
let concat_liquid_colors ((lamp1 : lava_lamp), (lamp2 : lava_lamp)) =
lamp1.color_liquid ^ " " ^ lamp2.color_liquid
let epsilon = 0.0001
let same_height (lamp1, lamp2) =
Float.abs (lamp1.height -. lamp2.height) < epsilon
(* Records and Variants *)
type myrecord = {
name: string;
height: int
}
let r = { name = "james"; height = 75}
let who = r.name
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment