PasteRack.org
Paste # 78141
2021-11-21 18:58:07

Fork as a new paste.

Paste viewed 1139 times.


Embed:

autograder using built-in flatten instead of mine

  1. #lang racket
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;     CS 381 - Programming Lab #4 ;
  4. ; ;
  5. ;  < your name >   ;
  6. ;  < your email >                  ;
  7. ; ;
  8. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  9.  
  10. ; your code here
  11. (define (member? e lst)
  12.   (cond
  13.     [(empty? lst)        #f]
  14.     [(= e (first lst))   #t]
  15.     [else
  16.      (member? e (rest lst))]))
  17.  
  18. ;; '(1 2 3)
  19. ;; check whether 1 is a member of '(2 3),
  20. ;;   the rest of the list
  21. (define (set? lst)
  22.   (cond
  23.     [(empty? lst) #t]
  24.     [(member? (first lst) (rest lst)) #f]
  25.     [else
  26.      (set? (rest lst))]))
  27.  
  28. ;; '(1 2 3) '(4 5 6)  => '(1 2 3 4 5 6)
  29. ;; '(1 2)     '(2 3)  => '(1 2 3)
  30. ;; '()        '(2 3)  => '(2 3)
  31. ;; '(1 2)        '()  => '(1 2)
  32. ;; '(1 2) '(2 3)      => '(1 2 3)
  33. ;; '(1 2 3) '(1 3)      => '(1 2 3)
  34. (define (union lst1 lst2)
  35.   (cond
  36.     ;;[condition expression]
  37.     [(empty? lst1) lst2]
  38.     [(empty? lst2) lst1]
  39.     ;; check whether the first element of lst1
  40.     ;;  appears in lst2.
  41.     ;;  if it does, then don't add it to the output
  42.     ;;  if it doesn't, then include it in the output
  43.     [(member? (car lst1) lst2) (union (cdr lst1) (lst2))]
  44.  
  45.     ))
  46.  
  47. ;;(and condition0 condition1 etc)
  48. ;; (cond
  49. ;;   [(and (empty? lst0) (empty? lst1))  '()]
  50. ;;   )
  51.  
  52. ;; (flatten-one '((1) 2)) #=> '(1 2)
  53. ;; (flatten-one '()) #=> '()
  54. ;; (flatten-one '(1)) #=> '(1)
  55. ;; (flatten-one '(1 2)) #=> '(1 2)
  56. ;; (flatten-one '(() 2)) #=> '(2)
  57. (define (flatten-one lst)
  58.   (cond
  59.     [(empty? lst) '()]
  60.     [(list? (first lst)) (append (flatten-one (first lst))
  61.                                  (flatten-one (rest lst)))]
  62.     [else
  63.      (cons (first lst) (flatten-one (rest lst)))]))
  64.  
  65. (flatten-one '())
  66.  
  67. (define (flatten lst1 lst2)
  68.   (append (flatten-one lst1) (flatten-one lst2)))
  69.  
  70. ;;;;;;;;;;;;;;;;;
  71. ;  DO NOT EDIT  ;
  72. ;;;;;;;;;;;;;;;;;
  73. ; enables testing via piping in stdin
  74. (define-namespace-anchor anc)
  75. (define ns (namespace-anchor->namespace anc))
  76. (let loop ()
  77.   (define line (read-line (current-input-port) 'any))
  78.   (if (eof-object? line)
  79.     (display "")
  80.     (begin (print (eval (read (open-input-string line)) ns)) (newline) (loop))))
  81.  
  82. #|
  83.  
  84. flatten: arity mismatch;
  85. the expected number of arguments does not match the given number
  86. expected: 1
  87. given: 2
  88. arguments...:
  89. '(1 (2 3) 5)
  90. '(8 (13 (21 34) 55))
  91. context...:
  92. /autograder/source/lab4.rkt:115:0: loop
  93. /autograder/source/lab4.rkt: [running body]
  94.  
  95. |#

=>

'()