While doing the problems at Project Euler may not be considered work,
I can say that LOOP makes some of those puzzles easy to come up with reasonable answers. Something like
(loop for i from 1 to 1000 sum (expt i i))
sure is easy to type at the REPL.
I would take the time to learn the basics of LOOP, at the very least.
3. Loop is very powerful, granted, and many people are trying to
argue that "you can do so much with loop that it's unreadable."
This is not an argument.
While doing the problems at Project Euler may not be considered work,
I can say that LOOP makes some of those puzzles easy to come up with reasonable answers. Something like
(loop for i from 1 to 1000 sum (expt i i))
sure is easy to type at the REPL.
I would take the time to learn the basics of LOOP, at the very least.
3. Loop is very powerful, granted, and many people are trying to
argue that "you can do so much with loop that it's unreadable."
This is not an argument.
(loop for x below 10 collect x)
(loop for x in '(3 5 7)
for y in '(2 5 8)
when (= x y)
collect (cons x y))
are examples of loops that are really quite clear and nothing to be seeking more complicated ways to say just to prove they can do it a more "lispy" way. To be "lispy" is NOT to "seek the obscure".
3. Loop is very powerful, granted, and many people are trying to
argue that "you can do so much with loop that it's unreadable."
This is not an argument.
(loop for x in '(3 5 7)
for y in '(2 5 8)
when (= x y)
collect (cons x y))
((5 . 5))
If there is only 1 loop, then this is the way to do it.
(mapcan
(lambda(x y) (and (= x y) (cons x y)))
'(3 5 7) '(2 5 8))
===>
(5 . 5)
((5 . 5) (11 . 11))
(5 11 . 11)
((5 . 5) (11 . 11))
Ken Tilton wrote:
Anyway, I whipped one up in a few lines that not only pulls n items
off the list, but it returns the remaining items as well. Let me know what you think. I was kind of surprised that I was able to do it without the use of an accumulator parameter.
(defun first-n (n list)
"Splits the list after the first n elements"
(if (= 0 n)
(values '() list)
(multiple-value-bind (from-front rest) (first-n (1- n) (rest
list))
(values (cons (first list) from-front) rest))))
I would probably do something like this instead:
(defun first-n (n list)
(loop for i below n for (a . d) on list
collect a into x
finally (return (values x d))))
Niiiiiiice.
(loop for x in '(3 5 7)
for y in '(2 5 8)
when (= x y)
collect (cons x y))
(loop for x in '(3 5 7)
for y in '(2 5 8)
when (= x y)
collect (cons x y))
What if there were more than two lists?
This will handle any number of lists:
Scheme:
(filter-map
(lambda xs (and (apply = xs) xs))
'(0 3 5 7 9)
'(0 2 5.0 8 9.0)
'(2 4 5 8 9))
((5 5.0 5) (9 9.0 9))
(mappend [iff = [chain list list]]'(0 3 5 7 9)
At least that's the way it was in R5RS. I don't have a more recent
version of the specification handy.
Anyway, I whipped one up in a few lines that not only pulls n items
off the list, but it returns the remaining items as well. Let me know >>what you think. I was kind of surprised that I was able to do it
without the use of an accumulator parameter.
(defun first-n (n list)
"Splits the list after the first n elements"
(if (= 0 n)
(values '() list)
(multiple-value-bind (from-front rest) (first-n (1- n) (rest
list))
(values (cons (first list) from-front) rest))))
I would probably do something like this instead:
(defun first-n (n list)
(loop for i below n for (a . d) on list
collect a into x
finally (return (values x d))))
Niiiiiiice.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 65 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 05:36:23 |
| Calls: | 862 |
| Files: | 1,311 |
| D/L today: |
921 files (14,318M bytes) |
| Messages: | 264,603 |