[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Scanl for (emacs) Lisp?
From: |
pgt |
Subject: |
Scanl for (emacs) Lisp? |
Date: |
Sun, 20 Feb 2022 16:23:45 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) |
Hi,
surprisingly I couldn't find a function equivalent to scanl in swi-prolog[1]
or even the more sophisticated cascade[2] in UCB Logo. It is a higher
order function on a sequence, a mixture of folding and mapping. The seq,
dash and CL libs do not have it. Listopia, a CL lib has it[3].
One could use such function for example solving the famous trapping rain
water problem. A simple recursive implementation on a list could be:
(defun scanl (fn lst ini)
"returns a list of successive reduced values from the left"
(named-let scanl ((lst lst)
(ini ini)
(res nil))
(if lst
(scanl (cdr lst)
(setq ini (funcall fn ini (car lst)))
(append res (list ini)))
res)))
(scanl #'max '(3 1 4 5 2) 0)
=> (3 3 4 5 5)
Footnotes:
[1] https://www.swi-prolog.org/pldoc/man?predicate=scanl/4
[2] https://github.com/jrincayc/ucblogo-code/blob/master/helpfiles/cascade
[3] https://github.com/Dimercel/listopia#scans
Best regards
--
Plamen Tanovski
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Scanl for (emacs) Lisp?,
pgt <=