Guest User

Untitled

a guest
Jun 24th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. ;; Emily Stevens
  2. ;; Notes
  3.  
  4. ;; Data Types
  5. ;; number, pictures, string, boolean
  6.  
  7. ;; STRUCTURES :D
  8.  
  9. ;; a structure can have an infinite number of things in it
  10. ;; promt: write up a spreadsheet of a bunch of numbers
  11. ;; questions: how many numbers?
  12.  
  13.  
  14. ;; arbitrary length data: ability to grow
  15. ;; ex. Grocery list
  16. ;; grocery list with nothing on it = empty
  17. ;; grocery list with milk on it = (cons "milk" empty)
  18. ;; grocery list with milk and eggs on it = (cons "milk" (cons "eggs" empty))
  19. ;; grocery list with milk, eggs, chocolate = (cons "milk" (cons "eggs" (cons "chocolate" empty))
  20.  
  21.  
  22. ;; cons: any list -> list
  23.  
  24. (define STS (cons "Emily" (cons "Jordan" (cons "Ashley" empty))))
  25.  
  26. ;; how to take apart a list
  27.  
  28. ;; (first STS) -> "Emily"
  29. ;; (rest STS) -> (cons "Jordan" (cons "Spencer" (cons "Ashley" empty)))
  30.  
  31. ;;(first (rest (rest STS))) -> "Spencer"
  32.  
  33. ;; (first (rest STS)) -> "Jordan"
  34.  
  35. ;; (first (rest (rest (rest STS)))) -> "Ashley"
  36.  
  37. ;; first: list: -> any
  38. ;; rest: list: -> any
  39.  
  40. ;; (cons 13 (cons 2 empty))
  41.  
  42. ;; A list-of-number is:
  43. ;; empty
  44. ;; (cons number list-of-number)
  45.  
  46. ;; (cons 13 (cons 2 empty)))
  47.  
  48. ;; defining something using a the word in the defintion is called a recursive definition.
  49.  
  50.  
  51. ; ;; Template
  52. ; ;;lon-fun: list-of-number -> ?
  53. ; ;; given ..., produces ...
  54. ; (define (lon-fun alon)
  55. ; (cond
  56. ; [(empty? alon) ]
  57. ; [(cons? alon) (first alon)
  58. ; (lon-fun (rest alon))]))
  59.  
  60.  
  61.  
  62.  
  63. ;; Template
  64. ;;sum: list-of-number -> number
  65. ;; given a list of numbers, produces their sum
  66. (define (sum alon)
  67. (cond
  68. [(empty? alon) 0]
  69. [(cons? alon) ( + (first alon)
  70. (sum (rest alon)))]))
  71.  
  72. (check-expect (sum empty) 0)
  73. (check-expect (sum (cons 7 (cons 4 (cons -1 (cons 3 empty))))) 13)
  74.  
  75. ; (cons (7 (cons 4 (cons -1 empty))))
  76. ; (+ 7 (sum (cons 4 (cons -1 empty))))
  77. ; (+ 7 (+ 4 (sum (cons -1 empty))))
  78. ; (+ 7 (+ 4 (+ -1 (sum empty))))
  79. ; (+ 7 (+ 4 (+ -1 0)))
  80. ; (+ 7 (+ 4 -1))
  81. ; (+ 7 3)
  82. ; 10
  83.  
  84.  
  85. (define SUBJECTS (cons "Math" (cons "Science" (cons "English" (cons "Social Studies" empty)))))
  86. (define TAXONOMY (cons "kingdom" (cons "phylum" (cons "class" (cons "order" (cons "family" (cons "genus" (cons "species" empty))))))))
  87. (define FIBONACCI (cons 0 (cons 1 (cons 1 (cons 2 (cons 3 (cons 5 (cons 8 (cons 13 empty)))))))))
  88.  
  89. ; A list-of-number is:
  90. ; empty, or
  91. ; (cons number list-of-number)
  92.  
  93.  
  94. ; lon-fun: list-of-number -> ?
  95. ; given ..., produces ...
  96. (define (lon-fun alon)
  97. (cond
  98. [(empty? alon) ]
  99. [(cons? alon) (first alon)
  100. (lon-fun (rest alon))]))
Add Comment
Please, Sign In to add comment