Skip to content

Commit

Permalink
Implement map in the more obvious way
Browse files Browse the repository at this point in the history
The most obvious way to do it is frankly a little bit faster.

I originally planned to use concat with left-fold because I wanted to
rely on tail call optimization, but I didn't realize how that wouldn't
work in that case.

This way still requires the stack, unfortunately. I'm trying to think
of whether there might be a way to implement some kind of tail
optimization with cons specifically so that list generating functions
don't overflow the stack, but I'm not quite sure how to make that happen
yet.
  • Loading branch information
devyn committed Aug 16, 2023
1 parent 59b8ef3 commit 7afd8e9
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions stage2/04-functional.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@

; functional map list
(define map (fn (f list)
(left-fold
(fn (out-list val)
(concat out-list (cons (f val) ())))
()
list)))
(if (nil? list) ()
(cons (f (car list)) (map f (cdr list))))))

; let multiple
; e.g. (let ((foo 1) (bar 2)) (+ foo bar))
Expand Down

0 comments on commit 7afd8e9

Please sign in to comment.