From ff60d0d3daed364588187b523a903a9abbdc2a4a Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Wed, 24 Jan 2018 20:28:29 -0500 Subject: [PATCH] Add MAP and FLAT-MAP functions --- lisp/lang/functions.lisp | 101 +++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 37 deletions(-) diff --git a/lisp/lang/functions.lisp b/lisp/lang/functions.lisp index d00dffc..d222b99 100644 --- a/lisp/lang/functions.lisp +++ b/lisp/lang/functions.lisp @@ -1,39 +1,66 @@ -(defun mapcar (function-name the-list) - (if the-list - (cons - (funcall function-name (first the-list)) +(defun mapcar (function-name the-list) + (if the-list + (cons + (funcall function-name (first the-list)) (mapcar function-name (rest the-list))))) -(defun maplist (function-name the-list) - (cond - ((null the-list) nil) - (t (cons (funcall function-name the-list) - (maplist function-name (rest the-list)))))) - -(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))))) +(defun maplist (function-name the-list) + (cond + ((null the-list) nil) + (t (cons (funcall function-name 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) + (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)))))