fork download
  1. (def null?
  2. (fn [a]
  3. (or
  4. (nil? a)
  5. (= () a))))
  6.  
  7. (def member?
  8. (fn [a lat]
  9. (cond
  10. (null? lat) false
  11. true (or
  12. (= (first lat) a)
  13. (member? a (rest lat)))) ))
  14.  
  15. (println "")
  16. (println "member?")
  17. (println (member? 'sweet '(potato wedges with sweet chilli sauce))) ; //=> true
  18. (println (member? 'sweet '(potato wedges with sweet sweet chilli sauce))) ; //=> true
  19.  
  20. (def rember
  21. (fn [ a lat]
  22. (cond
  23. (null? lat) '()
  24. true (cond
  25. (= (first lat) a) (rest lat)
  26. true (cons (first lat)
  27. (rember
  28. a (rest lat)))))))
  29.  
  30. (println "")
  31. (println "rember")
  32. (println (rember 'banana '(apple banana orange))) ; //=> (apple orange)
  33. (println (rember 'banana '(apple banana banana orange))); //=> (apple banana orange)
  34.  
  35. (def multirember
  36. (fn [a lat]
  37. (cond
  38. (null? lat) '()
  39. true (cond
  40. (= (first lat) a) (multirember a (rest lat))
  41. true (cons (first lat) (multirember a (rest lat)))))))
  42.  
  43. (println "")
  44. (println "multirember")
  45. (println (multirember 'sweet '(potato wedges with sweet sweet chilli sauce))); //=> (potato wedges with chilli sauce)
  46.  
  47. (def multiinsertR
  48. (fn [new old lat]
  49. (cond
  50. (null? lat) '()
  51. true (cond
  52. (= (first lat) old) (cons (first lat) (cons new (multiinsertR new old (rest lat))))
  53. true (cons (first lat) (multiinsertR new old (rest lat)))))))
  54.  
  55. (println "")
  56. (println "multiInsertR")
  57. (println (multiinsertR 'hot 'with '(with potato wedges with chilli sauce))); //=> (with hot potato wedges with hot chilli sauce)
  58.  
  59. (def multiinsertL
  60. (fn [new old lat]
  61. (cond
  62. (null? lat) '()
  63. true (cond
  64. (= (first lat) old) (cons new (cons old (multiinsertL new old (rest lat))))
  65. true (cons (first lat) (multiinsertL new old (rest lat)))))))
  66.  
  67. (println "")
  68. (println "multiinsertL")
  69. (println (multiinsertL 'with 'hot '(hot potato wedges hot chilli sauce))); //=> (with hot potato wedges with hot chilli sauce)
  70.  
  71. (def multisubst
  72. (fn [new old lat]
  73. (cond
  74. (null? lat) '()
  75. true (cond
  76. (= (first lat) old) (cons new (multisubst new old (rest lat)))
  77. true (cons (first lat) (multisubst new old (rest lat)))))))
  78.  
  79. (println "")
  80. (println "multisubst")
  81. (println (multisubst 'hot 'sweet '(sweet potato wedges with sweet chilli sauce))); //=> (hot potato wedges with hot chilli sauce)
  82.  
  83. (def add1
  84. (fn [n]
  85. (+ 1 n)))
  86.  
  87. (def occur
  88. (fn [a lat]
  89. (cond
  90. (null? lat) 0
  91. true (cond
  92. (= (first lat) a) (add1 (occur a (rest lat)))
  93. true (occur a (rest lat))))))
  94.  
  95. (println "")
  96. (println "occur")
  97. (println (occur 'hot '(potato wedges with hot hot hot chilli sauce))); //=> 3
  98.  
  99. (def one?
  100. (fn [n]
  101. (= n 1)))
  102.  
  103. (def sub1
  104. (fn [n]
  105. (- n 1)))
  106.  
  107. (def rempick
  108. (fn [n lat]
  109. (cond
  110. (null? lat) '()
  111. (one? n) (rest lat)
  112. true (cons (first lat) (rempick (sub1 n) (rest lat))))))
  113.  
  114. (println "")
  115. (println "rempick")
  116. (println (rempick 4 '(potato wedges with hot chilli sauce))); //=> (potato wedges with chilli sauce)
  117.  
  118.  
Success #stdin #stdout 1.31s 220224KB
stdin
Standard input is empty
stdout
member?
true
true

rember
(apple orange)
(apple banana orange)

multirember
(potato wedges with chilli sauce)

multiInsertR
(with hot potato wedges with hot chilli sauce)

multiinsertL
(with hot potato wedges with hot chilli sauce)

multisubst
(hot potato wedges with hot chilli sauce)

occur
3

rempick
(potato wedges with chilli sauce)