This is my solution to Ex. 5 on p. 97 of Paul Graham's "ANSI Common
Lisp"
<QUOTE>
Define iterative and recursive versions of a function that takes an
object x and a vector v, and returns a list of all the objects that immediately precede x in v.
(precedes #\a "abracadabra")(#\c #\d #\r)
</QUOTE>
(defun precedes (object vector)
(do ((length (length vector))
(results nil)
(idx 1 (1+ idx)))
((= idx length) results)
(when (eql object (aref vector idx))
(pushnew (aref vector (1- idx)) results))))
I don't think that's really any better. Maybe LOOP:
(defun precedes (object vector)
(loop with results = nil
for idx from 1 below (length vector)
when (eql object (aref vector idx))
do (pushnew (aref vector (1- idx)) results)
finally (return results)))
Define iterative and recursive versions of a function that takes an
object x and a vector v, and returns a list of all the objects that immediately precede x in v.
Graham doesn't like LOOP, but I do. So here's a LOOP-version:
(defun precedes (object vector)
(loop for x across vector
and i from 0
when (and (equal x object) (> i 0))
collect (elt vector (1-i))))
Arthur Lemmens wrote:
Define iterative and recursive versions of a function that takes an
object x and a vector v, and returns a list of all the objects that
immediately precede x in v.
Graham doesn't like LOOP, but I do. So here's a LOOP-version:
(defun precedes (object vector)
(loop for x across vector
and i from 0
when (and (equal x object) (> i 0))
collect (elt vector (1-i))))
Look at that:(4 9)
(1-i)
Don't you think that that should be:
(1- i)
or
(- i 1)
?
It's shorter when you use a Lispy language instead of CL.
Gauche Scheme
(use srfi-42) ; list-ec
(define (precedes obj vec)
(list-ec (: x (index i) vec)
(if (and (> i 0) (equal? x obj)))
(ref vec (- i 1))))
(precedes 5 #(5 0 4 5 8 9 5))
(4 9)
Another way:
(define (precedes o v)
(let ((l (vector->list vec)))
(filter-map
(^(a b) (and (equal? o a) b))
(cdr l)
l)))
(match @(scan-all (@x 5 . @nil)) '(5 0 4 5 8 9 5) x)
(window-mappend 1 nil (do if (and @1 (= @2 5)) (list @1)) #(5 0 4 5 8 9 5))#(4 9)
Arthur Lemmens wrote:
Define iterative and recursive versions of a function that takes an object x and a vector v, and returns a list of all the objects that immediately precede x in v.
Graham doesn't like LOOP, but I do. So here's a LOOP-version:
(defun precedes (object vector)
(loop for x across vector
and i from 0
when (and (equal x object) (> i 0))
collect (elt vector (1-i))))
Look at that:
(1-i)
Don't you think that that should be:
(1- i)
or
(- i 1)
?
It's shorter when you use a Lispy language instead of CL.
Gauche Scheme
(use srfi-42) ; list-ec
(define (precedes obj vec)
(list-ec (: x (index i) vec)
(if (and (> i 0) (equal? x obj)))
(ref vec (- i 1))))
(precedes 5 #(5 0 4 5 8 9 5))
===>
(4 9)
Another way:
(define (precedes o v)
(let ((l (vector->list vec)))
(filter-map
(^(a b) (and (equal? o a) b))
(cdr l)
l)))
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 74 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 66:34:38 |
| Calls: | 1,032 |
| Calls today: | 3 |
| Files: | 1,332 |
| D/L today: |
6 files (45K bytes) |
| Messages: | 275,541 |