#lang racket #| Max Sherman CSE 341 Section 2 Things to do: 1. go over hw1 2. review recursive functions 3. lexical scope example 4. truthy falsy 5. delayed evaluation, function call semantics 6. streams if time |# ;; function to convert celsius to farenheit (me) (define (c->f ctemp) (+ (* 1.8 ctemp) 32)) ;; write map (them) ; (define (map f xs) .... ;; convert list of temperatures (define temps '(-40 20 30 100)) ; (map c->f temps) ; or alternatively with a lambda ; (map (lambda (ctemp) (+ (* 1.8 ctemp) 32)) temps) ; lexical scope example (define x 1) (define (f) (let ([x 2]) (g))) (define (g) x) ; what does (f) return? ; what if we change f so that it's (define (f) (set! x 2) (g)) ; what if we also change g so that it's (define (g) (let ([y x]) y)) ;; what values in racket are truthy and which are falsy? ;; function call semantics --> when called, the first thing functions do is ;; evaluate the expressions passed to them as arguments (define (ident x) x) ; what does (ident (+ (* 4 5) 9)) do? ; what about (ident (factorial 5)) ; what about (ident (begin (displayln "hi") 5)) (define (disregard-y x y) x) ; what does (disregard-y (begin (displayln "x") 1) (begin (displayln "y") 2)) do? ; how can we fix this if we dont want to print "y"? ; how can we write if in this way ;; streams (define (nats) (define (aux n) (cons n (lambda () (aux (add1 n))))) (aux 0)) (define (print-stream s) (let ([pr (s)]) (displayln (car pr)) (print-stream (cdr pr))))