Add MAP and FLAT-MAP functions
This commit is contained in:
parent
b5638d631b
commit
ff60d0d3da
|
@ -1,39 +1,66 @@
|
||||||
(defun mapcar (function-name the-list)
|
(defun mapcar (function-name the-list)
|
||||||
(if the-list
|
(if the-list
|
||||||
(cons
|
(cons
|
||||||
(funcall function-name (first the-list))
|
(funcall function-name (first the-list))
|
||||||
(mapcar function-name (rest the-list)))))
|
(mapcar function-name (rest the-list)))))
|
||||||
|
|
||||||
(defun maplist (function-name the-list)
|
(defun maplist (function-name the-list)
|
||||||
(cond
|
(cond
|
||||||
((null the-list) nil)
|
((null the-list) nil)
|
||||||
(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 append (list-one list-two)
|
(defun map (function the-list)
|
||||||
(append-tail (reverse list-one) list-two))
|
(reverse (map-tail function the-list nil)))
|
||||||
|
|
||||||
(defun append-tail (reversed-list list-two)
|
(defun map-tail (function the-list accumulator)
|
||||||
(if (null reversed-list) list-two
|
(if the-list
|
||||||
(recur (rest reversed-list) (cons (car reversed-list) list-two))))
|
(recur
|
||||||
|
function
|
||||||
(defun reverse (the-list)
|
(rest the-list)
|
||||||
(reverse-tail () the-list))
|
(cons (call function (first the-list)) accumulator))
|
||||||
|
accumulator))
|
||||||
(defun reverse-tail (accumulator the-list)
|
|
||||||
(if (null the-list) accumulator
|
(defun flat-map (function the-list)
|
||||||
(recur (cons (first the-list) accumulator) (rest the-list))))
|
(reverse (flat-map-tail function the-list nil)))
|
||||||
|
|
||||||
(defun deep-reverse (the-list)
|
(defun flat-map-tail (function the-list accumulator)
|
||||||
(if the-list
|
(if the-list
|
||||||
(append
|
(recur
|
||||||
(deep-reverse (rest the-list))
|
function
|
||||||
(list
|
(rest the-list)
|
||||||
(if (listp (first the-list))
|
(append
|
||||||
(deep-reverse (first the-list))
|
(let ((result (call function (first the-list))))
|
||||||
(first the-list))))))
|
(if (list? result)
|
||||||
|
(reverse result)
|
||||||
(defun nth (n listA)
|
(list result)))
|
||||||
(cond
|
accumulator))
|
||||||
((equal 0 n) (first listA))
|
accumulator))
|
||||||
(t (nth (- n 1) (rest listA)))))
|
|
||||||
|
(defun append (list-one list-two)
|
||||||
|
(append-tail (reverse list-one) list-two))
|
||||||
|
|
||||||
|
(defun append-tail (reversed-list list-two)
|
||||||
|
(if (null reversed-list) list-two
|
||||||
|
(recur (rest reversed-list) (cons (car reversed-list) list-two))))
|
||||||
|
|
||||||
|
(defun reverse (the-list)
|
||||||
|
(reverse-tail () the-list))
|
||||||
|
|
||||||
|
(defun reverse-tail (accumulator the-list)
|
||||||
|
(if (null the-list) accumulator
|
||||||
|
(recur (cons (first the-list) accumulator) (rest the-list))))
|
||||||
|
|
||||||
|
(defun deep-reverse (the-list)
|
||||||
|
(if the-list
|
||||||
|
(append
|
||||||
|
(deep-reverse (rest the-list))
|
||||||
|
(list
|
||||||
|
(if (listp (first the-list))
|
||||||
|
(deep-reverse (first the-list))
|
||||||
|
(first the-list))))))
|
||||||
|
|
||||||
|
(defun nth (n listA)
|
||||||
|
(cond
|
||||||
|
((equal 0 n) (first listA))
|
||||||
|
(t (nth (- n 1) (rest listA)))))
|
||||||
|
|
Loading…
Reference in New Issue