Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; Emily Stevens
- ;; Notes
- ;; Data Types
- ;; number, pictures, string, boolean
- ;; STRUCTURES :D
- ;; a structure can have an infinite number of things in it
- ;; promt: write up a spreadsheet of a bunch of numbers
- ;; questions: how many numbers?
- ;; arbitrary length data: ability to grow
- ;; ex. Grocery list
- ;; grocery list with nothing on it = empty
- ;; grocery list with milk on it = (cons "milk" empty)
- ;; grocery list with milk and eggs on it = (cons "milk" (cons "eggs" empty))
- ;; grocery list with milk, eggs, chocolate = (cons "milk" (cons "eggs" (cons "chocolate" empty))
- ;; cons: any list -> list
- (define STS (cons "Emily" (cons "Jordan" (cons "Ashley" empty))))
- ;; how to take apart a list
- ;; (first STS) -> "Emily"
- ;; (rest STS) -> (cons "Jordan" (cons "Spencer" (cons "Ashley" empty)))
- ;;(first (rest (rest STS))) -> "Spencer"
- ;; (first (rest STS)) -> "Jordan"
- ;; (first (rest (rest (rest STS)))) -> "Ashley"
- ;; first: list: -> any
- ;; rest: list: -> any
- ;; (cons 13 (cons 2 empty))
- ;; A list-of-number is:
- ;; empty
- ;; (cons number list-of-number)
- ;; (cons 13 (cons 2 empty)))
- ;; defining something using a the word in the defintion is called a recursive definition.
- ; ;; Template
- ; ;;lon-fun: list-of-number -> ?
- ; ;; given ..., produces ...
- ; (define (lon-fun alon)
- ; (cond
- ; [(empty? alon) ]
- ; [(cons? alon) (first alon)
- ; (lon-fun (rest alon))]))
- ;; Template
- ;;sum: list-of-number -> number
- ;; given a list of numbers, produces their sum
- (define (sum alon)
- (cond
- [(empty? alon) 0]
- [(cons? alon) ( + (first alon)
- (sum (rest alon)))]))
- (check-expect (sum empty) 0)
- (check-expect (sum (cons 7 (cons 4 (cons -1 (cons 3 empty))))) 13)
- ; (cons (7 (cons 4 (cons -1 empty))))
- ; (+ 7 (sum (cons 4 (cons -1 empty))))
- ; (+ 7 (+ 4 (sum (cons -1 empty))))
- ; (+ 7 (+ 4 (+ -1 (sum empty))))
- ; (+ 7 (+ 4 (+ -1 0)))
- ; (+ 7 (+ 4 -1))
- ; (+ 7 3)
- ; 10
- (define SUBJECTS (cons "Math" (cons "Science" (cons "English" (cons "Social Studies" empty)))))
- (define TAXONOMY (cons "kingdom" (cons "phylum" (cons "class" (cons "order" (cons "family" (cons "genus" (cons "species" empty))))))))
- (define FIBONACCI (cons 0 (cons 1 (cons 1 (cons 2 (cons 3 (cons 5 (cons 8 (cons 13 empty)))))))))
- ; A list-of-number is:
- ; empty, or
- ; (cons number list-of-number)
- ; lon-fun: list-of-number -> ?
- ; given ..., produces ...
- (define (lon-fun alon)
- (cond
- [(empty? alon) ]
- [(cons? alon) (first alon)
- (lon-fun (rest alon))]))
Add Comment
Please, Sign In to add comment