Skip to content

Commit

Permalink
Added intersection, member, and union functions.
Browse files Browse the repository at this point in the history
These are neat to have.
  • Loading branch information
skx committed Nov 6, 2022
1 parent 86afc13 commit de2e700
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions PRIMITIVES.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ Functions here include:
* Is the given thing a hash?
* `inc`
* Increment the given variable.
* `intersection`
* Return those elements in common in the specified pair of lists.
* `last`
* Return the last element of the specified list.
* `length`
Expand All @@ -304,6 +306,8 @@ Functions here include:
* Return the results of applying the specified function to every pair of elements in the given list.
* `max`
* Return the maximum value in the specified list.
* `member`
* Return true if the specified item is contained within the given list.
* `min`
* Return the maximum value in the specified list.
* `nat`
Expand Down Expand Up @@ -362,6 +366,8 @@ Functions here include:
* `translate`
* Translate a string of characters, via a lookup table.
* Used by `lower`, and `upper`.
* `union`
* Return a list of all items in the specified two lists - without duplicates.
* `upper`
* Return an upper-case version of the specified string.
* `upper-table`
Expand Down
39 changes: 39 additions & 0 deletions stdlib/stdlib/lists.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
;;; lists.lisp - Some list-utility functions

;; These were adapted from Rob Pike's lisp
;;
;; https://github.com/robpike/lisp
;;
;; which in turn were derived from code in "LISP 1.5 Programmer's Manual"
;; by McCarthy, Abrahams, Edwards, Hart, and Levin, from MIT in 1962.
;;


(set! member (fn* (item list)
"Return true if the specified item is found within the given list.
See-also: intersection, union."

(cond
(nil? list) false
(eq item (car list)) true
true (member item (cdr list)))))

(set! union (fn* (x y)
"Return the union of the two specified lists.
See-also: intersection, member"
(cond
(nil? x) y
(member (car x) y) (union (cdr x) y)
true (cons (car x) (union (cdr x) y))
)))

(set! intersection (fn* (x y)
"Return the values common to both specified lists
See-also: member, union."
(cond
(nil? x) nil
(member (car x) y) (cons (car x) (intersection (cdr x) y))
true (intersection (cdr x) y))))

0 comments on commit de2e700

Please sign in to comment.