PasteRack.org
Paste # 41712
2020-09-26 12:15:32

Fork as a new paste.

Paste viewed 542 times.


Embed:

  1. #lang racket #| CSC324 Fall 2020: Exercise 2 |#
  2. #|
  3.  
  4. |#
  5. ;-------------------------------------------------------------------------------
  6. ; This expression exports functions so they can be imported into other files.
  7. ; Don't change it!
  8. (provide apply-fns make-counter apply-fns sum-map-both sum-map-both-helper calculate)
  9.  
  10. (module+ test
  11.   ; Import the testing library
  12.   (require rackunit))
  13.  
  14. ;-------------------------------------------------------------------------------
  15. ; * Task 1: num-pred *
  16. ;-------------------------------------------------------------------------------
  17.  
  18. #|
  19. (num-pred pred lst)
  20.   pred: A function that takes one argument and returns a boolean
  21.   lst: A list of items.
  22.  
  23.   Returns the number of items in the lst for which (pred item) returns True
  24. |#
  25. (define (num-pred pred lst)
  26.   (length (filter pred lst)))
  27.  
  28.  
  29. ;-------------------------------------------------------------------------------
  30. ; * Task 2: More Higher-Order Functions *
  31. ;-------------------------------------------------------------------------------
  32.  
  33. #|(define (make-counter-helper pred acc)
  34.   (if (null? list)
  35.       0
  36.       (if (pred
  37. |#
  38.  
  39. #|
  40. (make-counter pred)
  41.   pred: A function that takes one argument and returns a boolean
  42.  
  43.   Returns a function that takes a list and returns the number of elements
  44.   in the list satisfying that predicate.
  45. |#
  46.  
  47. (define (make-counter pred)
  48.   (lambda (lst) (length (filter pred lst))))
  49.  
  50.  
  51. ; Uncomment and define these using `make-counter`.
  52. (define num-evens-1 (make-counter even?))
  53. (define num-many-evens-1 (lambda (lists)
  54.                            (cond
  55.                              [(null? lists) 0]
  56.                              [else
  57.                               (let ([first-list (num-evens-1 (first lists))]
  58.                                     [rest-list (num-many-evens-1 (rest lists))])
  59.                                 (if (< first-list 3)
  60.                                     rest-list
  61.                                     (+ rest-list 1)))])))
  62.  
  63. (module+ test
  64.   ; TODO: write your own tests to make sure make-counter is implemented correctly.
  65.   (test-equal? "num-evens-1: empty list"
  66.                (num-evens-1 null)
  67.                0)
  68.   (test-equal? "num-evens: simple non-empty list"
  69.                (num-evens-1 (list 1 2 4))
  70.                2)
  71.   (test-equal? "num-many-evens-1: empty list"
  72.                (num-many-evens-1 null)
  73.                0)
  74.   (test-equal? "num-many-evens-1: simple non-empty list"
  75.                (num-many-evens-1 (list (list 2 4 5 7 8)))
  76.                1)
  77.  
  78. )
  79.  
  80.  
  81. #|
  82. (apply-fns fns arg)
  83.   fns: A list of unary functions.
  84.   arg: A value.
  85.  
  86.   Returns a list of the results of applying each function in `fns` to `arg`.
  87. |#
  88. (define (apply-fns fns arg)
  89.   ; TODO: replace the (void) with a proper function body.
  90.   (if (null? fns)
  91.       '()
  92.       (cons ((first fns) arg) (apply-fns (rest fns) arg))))
  93.  
  94. (module+ test
  95.   (test-equal? "(apply-fns (list integer? null?) 4)"  ; Test label
  96.                (apply-fns (list integer? null?) 4)   ; Actual value
  97.                (list #t #f))                         ; Expected value
  98.   ; TODO: write more tests
  99.   (test-equal? "(apply-fns (list number? null? zero? positive? negative? 5)"
  100.                (apply-fns (list number? null? zero? positive? negative?) 5)
  101.                (list #t #f #f #t #f))
  102. )
  103.  
  104. #|
  105. (sum-map-both fn lst1 lst2)
  106.   fn: A function that takes two arguments and returns a number.
  107.   lst1: A list
  108.   lst2: Another list of equal length
  109.  
  110.   Precondition: (equal? (length lst1) (length lst2))
  111.   You do not need to check the pre-condition. We will only be testing
  112.   with tests that satisfy the pre-condition.
  113.  
  114.   Returns the sum of the `fn` applied to each of the pairs of values
  115.   in lst1 and lst2. We would like this function to be *tail-recursive*,
  116.   so it should call the helper function `sum-map-both-helper` with
  117.   an appropriate initial accumulator value.
  118. |#
  119. (define (sum-map-both fn lst1 lst2)
  120.   (sum-map-both-helper fn lst1 lst2 0))
  121.   ; (sum-map-both-helper fn lst1 lst2 TODO))
  122.  
  123.  
  124. #|
  125. (sum-map-both-helper fn lst1 lst2 acc)
  126.   fn: A function that takes
  127.   lst1: A list
  128.   lst2: Another list of equal length
  129.   acc: A number describing the accumulated values so far
  130.  
  131.   Precondition: (equal? (length lst1) (length lst2))
  132.   You do not need to check the pre-condition. We will only be testing
  133.   with tests that satisfy the pre-condition.
  134.  
  135.   Helper function for sum-map-both-helper. We *will* be testing this
  136.   helper function.
  137. |#
  138. (define (sum-map-both-helper fn lst1 lst2 acc)
  139.   (if (null? lst1)
  140.       0
  141.       (sum-map-both-helper fn (rest lst1) (rest lst2) (+ (fn (first lst1) (first lst2)) acc))))
  142.  
  143. (module+ test
  144.   (test-equal? "(sum-map-both - '(5 10 7) '(2 6 5))" ; Test label
  145.                (sum-map-both - '(5 10 7) '(2 6 5))   ; Actual value
  146.                9)  ; Expected value = (5 - 2) + (10 - 6) + (7 - 5)
  147.   ; TODO: write more tests
  148. )
  149.  
  150. ;-------------------------------------------------------------------------------
  151. ; * Task 4: Calculator *
  152. ;-------------------------------------------------------------------------------
  153.  
  154. #|
  155. (calculate expr)
  156.   expr: An expression generated by the Binary Arithmetic Expression Grammar
  157.         described in the handout.
  158.  
  159.   Return the numerical value of the expression
  160. |#
  161. (define/match (calculate expr)
  162.   [((list '+ expr1 expr2)) (+ (calculate expr1) (calculate expr2))]
  163.   [((list '- expr1 expr2)) (- (calculate expr1) (calculate expr2))]
  164.   [((list '* expr1 expr2)) (* (calculate expr1) (calculate expr2))]
  165.   [((list '/ expr1 expr2)) (/ (calculate expr1) (calculate expr2))]
  166.   [(expr) expr])
  167.  
  168. (module+ test
  169.   (require rackunit)
  170.   (test-equal? "calculate: +"
  171.                (calculate '(+ 2 3)) ;'(+ 2 3) is the same as (list '+ 2 3)
  172.                5)
  173.   (test-equal? "calculate: /"
  174.                (calculate '(/ (+ 2 6) 2))
  175.                4))
  176.  

=>