list contains implemented

This commit is contained in:
Ava Apples Affine 2023-03-20 22:03:10 -07:00
parent da0709c4db
commit b638918a89
Signed by: affine
GPG key ID: 3A4645B8CF806069
7 changed files with 36 additions and 9 deletions

View file

@ -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
))

View file

@ -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))