list contains implemented
This commit is contained in:
parent
da0709c4db
commit
b638918a89
7 changed files with 36 additions and 9 deletions
|
|
@ -55,6 +55,18 @@
|
|||
(adder (lambda (x y) (add x y))))
|
||||
(eq? (reduce adder list) (add 1 2 3)))))
|
||||
|
||||
('contains? finds elem in list'
|
||||
(quote
|
||||
(contains? (1 2 3) 1)))
|
||||
|
||||
('contains? finds last elem in list'
|
||||
(quote
|
||||
(contains? (1 2 3) 3)))
|
||||
|
||||
('contains? doesnt find elem not in list'
|
||||
(quote
|
||||
(not (contains? (1 2 3) 4))))
|
||||
|
||||
;; add more test cases here
|
||||
))
|
||||
|
||||
|
|
|
|||
|
|
@ -85,3 +85,18 @@ this will continue iuntil the list is exhausted.'
|
|||
(set (q result) (func result current))
|
||||
(set (q list-iter) (pop remaining)))))
|
||||
result))
|
||||
|
||||
(def contains?
|
||||
'Takes two arguments: a list and an element.
|
||||
Returns true if the list contains the element.'
|
||||
(list elem)
|
||||
(let ((found false)
|
||||
(list-iter (pop list)))
|
||||
(while (and (gt? (len list-iter) 1)
|
||||
(not found))
|
||||
(let ((current (car list-iter))
|
||||
(remaining (cdr list-iter)))
|
||||
(if (eq? current elem)
|
||||
(set (q found) true)
|
||||
(set (q list-iter) (pop remaining)))))
|
||||
found))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue