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

L20 code

parent 9e7512e5
......@@ -48,6 +48,7 @@ TODAY:
- "combining functions"
- "let over lambda"
- refs and mutation
- name "let over lambda" comes from the syntax
*)
(* function composition: takes two functions and returns a function *)
......@@ -203,3 +204,23 @@ let setup_library () =
(insert, get_contents)
let (insert, get_contents) = setup_library ()
(* imagine this was the beginning of file*)
(* env = [] *)
let myinc =
let x = ref 0 in
(* current dynenv: [x -> "ref 0"]
more accurately [x -> some address A]
and in the heap [A -> 0]
*)
fun () ->
x := !x + 1;
print_int !x;
print_newline ()
(* evaluates to a closure with its code and defenv = [x -> "ref 0"] *)
(* env = [myinc -> that closure] *)
let _ = myinc ()
let _ = myinc ()
(define x 1)
; equivalent to:
; let f = fun y -> y + 1
; in OCaml
(define f (lambda (y) (+ 1 y)))
(f 1)
(define (map f l)
(match l
(nil nil)
((cons x xs) (cons (f x) (map f xs)))))
(map f (cons 1 (cons 2 (cons 3 nil))))
(map (lambda (z) (* z 10)) (cons 1 (cons 2 (cons 3 nil))))
(define curried-map (lambda (x) (lambda (y) (map x y))))
(define inc-every-element-by-two (curried-map (lambda (w) (+ w 2))))
(inc-every-element-by-two (cons 1 (cons 2 (cons 3 nil))))
; in trefoil, unlike OCaml, currying is not the default
; instead, the built-in multi-argument functions are used
; unless there is a special reason to curry something
;; new content introduced in trefoil!!!
(if true 0 1)
(if true 0 not-a-var) ; only one side of the branch is executed!
(define (countdown n)
(if (= n 0)
nil
(cons n (countdown (- n 1)))))
(countdown 10)
(define (my-if-broken branch then else)
(if branch then else))
(my-if-broken true 0 1)
(my-if-broken true 0 not-a-var) ; only one side of the branch is executed!
(if true 0 1)
(if true 0 not-a-var) ; only one side of the branch is executed!
(define (countdown n)
(if (= n 0)
nil
(cons n (countdown (- n 1)))))
(countdown 10)
(define (countdown n)
(my-if-broken (= n 0)
nil
(cons n (countdown (- n 1)))))
; would loop for ever
; (countdown 10)
;;;;;;;;;;;;;;;;;;;;;;;
; THUNKS
;;;;;;;;;;;;;;;;;;;;;;;
; thunk a made up word
;
; a function that takes zero arguments,
; whose only purpose is to delay the evaluation of its body
(define (my-if-fixed branch then else)
(if branch (then) (else)))
(my-if-fixed true (lambda () 0) (lambda () 1))
; only one side of the branch is executed!
(my-if-fixed true (lambda () 0) (lambda () not-a-var))
(define (countdown n)
(my-if-fixed (= n 0)
(lambda () nil)
(lambda () (cons n (countdown (- n 1))))))
; does not loop forever!
(countdown 10)
;;;;;;;;;;;;;;;;;;;;;;;;;
; STREAMS
;;;;;;;;;;;;;;;;;;;;;;;;;
; a stream represents an infinite sequence of items
; can't actually compute infinite amount of data, so trick:
; - delay most of it (using a thunk)
; slogan: a stream is a thunk that returns a pair
; in particular, a pair of the first element and another stream (thunk that...)
; return the infinite sequence 0, 0, 0, ...
(define (zeros)
(cons 0 (lambda () (zeros))))
; zeros is a thunk that returns a pair
; zeros is a stream
; first element
(car (zeros))
(let ((rest-of-stream (cdr (zeros)))) ; rest-of-stream is a stream (thunk that returns a pair)
(car (rest-of-stream)))
(define (all-natural-numbers-starting-at k)
(cons k (lambda () (all-natural-numbers-starting-at (+ k 1)))))
; return the infinite sequence 0, 1, 2, ...
(define (all-natural-numbers)
(all-natural-numbers-starting-at 0))
(car (all-natural-numbers))
(car ((cdr (all-natural-numbers))))
(car ((cdr ((cdr (all-natural-numbers))))))
(define first-pair (all-natural-numbers))
(define first-element (car first-pair))
first-element
(define rest-of-stream (cdr first-pair))
(define second-pair (rest-of-stream))
(define second-element (car second-pair))
second-element
(define rest-of-rest-of-stream (cdr second-pair))
(define third-pair (rest-of-rest-of-stream))
(define third-element (car third-pair))
third-element
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