PasteRack.org
Paste # 733
2021-11-21 19:05:45

Forked from paste # 78141.

Fork as a new paste.

Paste viewed 1159 times.


Embed:

autograder using built-in flatten instead of mine

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

=>

'()