Add MAP and FLAT-MAP functions

This commit is contained in:
Mike Cifelli 2018-01-24 20:28:29 -05:00
parent b5638d631b
commit ff60d0d3da
1 changed files with 64 additions and 37 deletions

View File

@ -10,6 +10,33 @@
(t (cons (funcall function-name the-list) (t (cons (funcall function-name the-list)
(maplist function-name (rest the-list)))))) (maplist function-name (rest the-list))))))
(defun map (function the-list)
(reverse (map-tail function the-list nil)))
(defun map-tail (function the-list accumulator)
(if the-list
(recur
function
(rest the-list)
(cons (call function (first the-list)) accumulator))
accumulator))
(defun flat-map (function the-list)
(reverse (flat-map-tail function the-list nil)))
(defun flat-map-tail (function the-list accumulator)
(if the-list
(recur
function
(rest the-list)
(append
(let ((result (call function (first the-list))))
(if (list? result)
(reverse result)
(list result)))
accumulator))
accumulator))
(defun append (list-one list-two) (defun append (list-one list-two)
(append-tail (reverse list-one) list-two)) (append-tail (reverse list-one) list-two))