Custopootimus

racket lists

Aug 23rd, 2013
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.47 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;;; Empty list base-case. Use as a basis for 'prepend'
  4. (define (empty selector)
  5.   (selector null null #t))
  6.  
  7. ;;; Prepend : prepend given element to list. use 'empty' as basis
  8. (define (prepend element list)
  9.   (lambda (selector)
  10.     (selector element list #f)))
  11.  
  12. ;;; Head : return first element in the list
  13. (define (head list)
  14.   (list (lambda (head tail e) head)))
  15.  
  16. ;;; Tail : return second element in the list, either a list, or null
  17. (define (tail list)
  18.   (list (lambda (head tail e) tail)))
  19.  
  20. ;;; empty? : return true if given list is empty
  21. (define (empty? list)
  22.   (list (lambda (head tail e) e)))
  23.  
  24. ;;; length : return length of given list
  25. (define (length list)
  26.   (cond
  27.     [(empty? list) 0]
  28.     [else (+ 1 (length (tail list)))]))
  29.  
  30. ;;; drop : return the list without the first n elements
  31. (define (drop list number)
  32.   (cond
  33.     [(= number 0) list]
  34.     [else (drop (tail list) (- 1 number))]))
  35.  
  36. ;;; take : return the list with only the first n elements
  37. (define (take list number)
  38.   (cond
  39.     [(= number 0) empty]
  40.     [else (prepend (head list) (take (tail list) (- 1 number)))]))
  41.  
  42. ;;; slice : retun the sub-list of a given start and end
  43. (define (slice list start end)
  44.   (take (drop list start) (- end start)))
  45.  
  46. ;;; get : retun the nth element of the list
  47. (define (get list number)
  48.   (cond
  49.     [(= 0 number) (head list)]
  50.     [else (drop (tail list (- 1 number)))]))
  51.  
  52. ;;; map : apply a function to each element of the list
  53. (define (map function list)
  54.   (cond
  55.     [(empty? list) list]
  56.     [else (prepend (function (head list)) (map function (tail list)))]))
  57.  
  58. ;;; filter : return a list containing elements only for which the given list returns true
  59. (define (filter function list)
  60.   (cond
  61.     [(empty? list) list]
  62.     [(function (head list)) (prepend (head list) (filter function (tail list)))]
  63.     [else (filter function (tail list))]))
  64.  
  65. ;;; contains? : return true if list contains a given element
  66. (define (contains? element list)
  67.   (cond
  68.     [(empty? list) #f]
  69.     [(= element (head list)) #t]
  70.     [else (contains? element (tail list))]))
  71.  
  72. ;;; containsAll? : return true if list contains all elements
  73. (define (containsAll? listA listB)
  74.   (cond
  75.     [(empty? listA) #t]
  76.     [(contains? (head listA)) (containsAll? tail(listA) listB)]
  77.     [else #f]))
  78.  
  79. (define (subset? listA listB)
  80.   (cond
  81.     [(and (empty? listA) (>= (length listB) (length listA)))]
  82.     [(contains? (head listA)) (subset? (tail listA) listB)]
  83.  
  84. (define l (prepend 5 empty))
Advertisement
Add Comment
Please, Sign In to add comment