Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
111FA16 Quiz 1 Review Session
; Conditionals
; (if <this> ; boolean
; <then-this>
; <else-this>)
; (if (and (> x a)
; (< x b))
; "x in range"
; "x out of range")
; + : number ... -> number
; and : boolean ... -> boolean
(and true true true)
; An example of using foldl with a function that isn't +
(define-struct birthday (day month year))
(define MY_BIRTHDAY (make-birthday 25 "December" 1))
(define BIRTHDAYS
(list MY_BIRTHDAY
(make-birthday 1 "April" 2016)
(make-birthday 5 "December" 2019)))
(foldl (lambda (current partial-sum)
(+ partial-sum
(birthday-day current)))
0
BIRTHDAYS)
; andmap : (a -> boolean), (listof a) -> boolean
; true iff (pred a) is true for all a in (listof a)
; ormap : (a -> boolean), (listof a) -> boolean
; true iff (pred a) is true for at least one a in (listof a)
; "Why have a lambda inside another lambda?"
(define-struct album (artist title genre))
(define DATABASE
(list (make-album ("Kidz Bop" "Kidz Bop 1" "Pop")
("Kidz Bop" "Kidz Bop 2" "Pop")
("Kidz Bop" "Kidz Bop 3" "Pop")
("Fall Out Boy" "Infinity on High" "Emo"))))
(define get-artist-albums
(lambda (db desired-artist)
(filter
; this thing needs to be a lambda inside another lambda...
(lambda (alb)
; ...so that we can use desired-artist inside the predicate
(eq? desired-artist
(album-artist alb)))
db)))
(get-artist-albums DATABASE "Kidz Bop")
; (local [<Definitions>
; <on>
; <definitions>]
; ...body...)
(define X 50)
(local [(define Y 20)]
(+ Y 1))
(define-struct birthday (day month year))
; increment-birth-year : birthday -> birthday
; returns a new birthday with the year incremented by one
(define increment-birth-year-inefficient
(lambda (bd)
(make-birthday (* 8 (+ (birthday-day bd) 3) (birthday-day bd))
(birthday-month bd)
(+ 1 (birthday-year bd)))))
(define increment-birth-year-better
(lambda (bd)
; using local definition to avoid repeatedly saying
; (birthday-day bd)
(local [(define birth-day (birthday-day bd))]
(make-birthday (* 8 (+ birth-day 3) birth-day)
(birthday-month bd)
(+ 1 (birthday-year bd))))))
(define scale-and-translate
(lambda (x)
(local [(define scale 50)]
(local [(define scale 3) ; shadowing outer definition
(define translation 1)]
(+ (* scale x) translation)))))
(+ 4 scale) ; scale: this variable is not defined
(scale-and-translate 60) ; 181
; Types and Signatures and Lambdas
(require 2htdp/image)
; ; Numbers
; 1
; -5
; 3.14
; pi
;
; ; Strings
; "this is a string"
; "a"
; "\"hello\""
; "5"
;
; ; Boolean
; true ; #true #t #T
; false ; #false #F #f
; Struct
; (define-struct <StructType> (<FieldName1> ... <FieldNameN>))
; a birthday is a struct: (make-birthday number string number)
(define-struct birthday (day month year))
; 1. "Maker": make-<StructType>
(define BIRTHDAY (make-birthday 5 "July" 1992))
; 2. "Validator": <StructType>?
(birthday? BIRTHDAY) ; true
(birthday? "hello") ; false
(birthday? (make-birthday 6 "January" 2014)) ; true
; 3. "Getter": <StructType>-<StructField>
(birthday-month BIRTHDAY) ; "July"
(birthday-month (make-birthday 6 "January" 2014)) ; "January"
; Function Signature
; f(x, y) = x^2 + 3 + 2y
; f : number, number -> number
; + : number ... -> number
; circle : number string string -> image
; map : (x -> y), (listof x) -> (listof y)
; increment-birth-year : birthday -> birthday
; returns a new birthday with the year incremented by one
(define increment-birth-year
(lambda (bd)
(make-birthday (birthday-day bd)
(birthday-month bd)
(+ 1 (birthday-year bd)))))
; Function calls
; (<FunctionName> <Input1> ... <InputN>)
; f(1)
; (1) = (1)^2 + 3
; 1 + 3 = 4
; f(1) = 4
; (apply <FunctionName> (list <Input1> ... <InputN>))
; Lambda
; f(x, y) = x^2 + 3 + 2y
; (x, y) = x^2 + 3 + 2y
(define f ; f
(lambda (x y) ; (x, y) =
(+ (sqr x) ; x^2 + 3 + 2y
3
(* 2 y))))
(lambda (x y)
(+ (sqr x)
3
(* 2 y)))
; f(2, 3) is the math equivalent of (f 2 3)
; which is the equivalent of
(apply f (list 2 3))
; f(5, 6) = 40
; (f(x, y) = x^2 + 3 + 2y)(5, 6)
((lambda (x y)
(+ (sqr x)
3
(* 2 y))) 5 6)
; f(5, 6) = (5)^2 + 3 + 2(6)
; (lambda (5 6)
; (+ (sqr 5) 3
; (* 2 6)))
; (5)^2 + 3 + 2(6)
(+ (sqr 5)
3
(* 2 6))
; 25 + 3 + 12
(+ 25
3
12)
; 40
(define HEIGHT 400)
(define g
(lambda (HEIGHT)
HEIGHT))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment