[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Newbie: reverse or append?

William James

8/4/2015 7:49:00 AM

Pascal Costanza wrote:

> (defun test3 (list index)
> (loop with at
> for element in list
> for i = 0 then (1+ i)
> if (< i index) collect element into before
> else if (= i index) do (setf at element)
> else if (> i index) collect element into after
> finally return (values before at after)))

Let's see if Armed Bear Common Lisp 0.12.0 accepts that crap:


CL-USER(1): (defun test3 (list index)
(loop with at
for element in list
for i = 0 then (1+ i)
if (< i index) collect element into before
else if (= i index) do (setf at element)
else if (> i index) collect element into after
finally return (values before at after)))
Debugger invoked on condition of type PROGRAM-ERROR:

Current LOOP context: "A compound form was expected, but ~S found.".


In "ANSI Common Lisp", Graham makes the following comments:

The loop macro was originally designed to help inexperienced
Lisp users write iterative code. Instead of writing Lisp code,
you express your program in a form meant to resemble English,
and this is then translated into Lisp. Unfortunately, loop is
more like English than its designers ever intended: you can
use it in simple cases without quite understanding how it
works, but to understand it in the abstract is almost
impossible.
....
the ANSI standard does not really give a formal specification
of its behavior.
....
The first thing one notices about the loop macro is that it
has syntax. A loop expression contains not subexpressions but
clauses. The clauses are not delimited by parentheses;
instead, each kind has a distinct syntax. In that, loop
resembles traditional Algol-like languages. But the other
distinctive feature of loop, which makes it as unlike Algol as
Lisp, is that the order in which things happen is only
loosely related to the order in which the clauses occur.
....
For such reasons, the use of loop cannot be recommended.


>
> If you think that this is too "unlispy", check out
> http://www.tfeb.org/lisp/hax.html#...
>
>
> Pascal
>
> Tyro wrote:
> > I wrote two recursive versions of a function that takes a list, an
> > index and an empty list and returns three values:
> > 1. The list of elements before the element at the given index
> > 2. The element at the given index
> > 3. The rest of the list after the given index
> >
> > For example, (test1 '(a b c d e f) 3 nil) would return
> > (A B C)
> > D
> > (E F)

Gauche Scheme:

(define (test3 the-list position)
(receive (a b) (split-at* the-list position)
(values a (list-ref b 0 #f) (drop* b 1))))


gosh> (test3 '(a b c d e) 3)
(a b c)
d
(e)
gosh> (test3 '(a b c d e) 4)
(a b c d)
e
()
gosh> (test3 '(a b c d e) 5)
(a b c d e)
#f
()
gosh> (test3 '(a b c d e) 6)
(a b c d e)
#f
()

--
I know it would be frightening for you to step outside the ever narrower
confines of what we are permitted to say about race.... Today, the most
dangerous ideas are the historical, biological, and moral truths that men such
as Washington, Jefferson, Teddy Roosevelt, Mark Twain, Walt Whitman, and your
grandparents took for granted. --- Jared Taylor