Add MAP and FLAT-MAP functions
This commit is contained in:
parent
b5638d631b
commit
ff60d0d3da
|
@ -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))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue