PasteRack.org
Paste # 50510
2021-11-21 18:54:24

Fork as a new paste.

Paste viewed 1126 times.


Embed:

  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.  
  72. TODO uncomment this code block
  73.  
  74. ;;;;;;;;;;;;;;;;;
  75. ;  DO NOT EDIT  ;
  76. ;;;;;;;;;;;;;;;;;
  77. ; enables testing via piping in stdin
  78. (define-namespace-anchor anc)
  79. (define ns (namespace-anchor->namespace anc))
  80. (let loop ()
  81.   (define line (read-line (current-input-port) 'any))
  82.   (if (eof-object? line)
  83.     (display "")
  84.     (begin (print (eval (read (open-input-string line)) ns)) (newline) (loop))))
  85. |#

=>

'()