Re: Hints on recursion
- From: Pascal Bourguignon <spam@xxxxxxxxxxxxxxxx>
- Date: Wed, 30 Nov 2005 06:18:30 +0100
"matteo d'addio 81" <m_a_t_e_81@xxxxxxxx> writes:
> Geoffrey Summerhayes ha scritto:
>
>> Prolog [Head | Tail], [A, B | Tail]
>> Lisp (car . cdr), (first second . rest)
>>
>> A list, e.g. '(1 2 3), is essentially shorthand for the dotted
>> equivalent,
>> in this case: '(1 . (2 . (3 . nil)))
>>
>
> Yes, I knew this, but...
>
>> So if we substitute a list, for example '(g h i), for rest into
>> Pascal's
>>
>> (twice 'e '(e . rest)) = (once 'e . rest)
>>
>> we get
>>
>> (twice 'e '(e g h i)) = (once 'e '(g h i))
>
> in my opinion if you substitute '(g h i) to rest you'll get:
>
> (twice 'e '(e quote (g h i))) = (once 'e quote (g h i))
>
> or:
>
> (twice 'e '(e g h i)) = (once 'e g h i)
>
> Am I wrong?
You're right. I'd substitute (g h i) to rest.
There's no clearly set conventions about what to write in prose.
If you want to mean a list containing the integers between 1 and 5 in
increasing order, how should I write it?
(1 2 3 #3r11 #4r11)
or:
(quote (#6r1 #5r2 #4r3 #3r11 #2r101))
When you try to evaluate the first list, it gives an error; when you
try to evaluate the second list (the one with two elements, the first
of which is the symbol named QUOTE and the second of which is a list
of 5 elements), then you get the first list!
[280]> (#6r1 #5r2 #4r3 #3r11 #2r101)
*** - EVAL: 1 is not a function name; try using a symbol instead
The following restarts are available:
USE-VALUE :R1 You may input a value to be used instead.
ABORT :R2 ABORT
Break 1 [281]> :q
[282]> (quote (#6r1 #5r2 #4r3 #3r11 #2r101))
(1 2 3 4 5)
Why should you evaluate (1 2 3 4 5) in the first place?
It's not a program, it's data!
Why, if I mean a list of 5 integers, should I write a list containing
a symbol and a sublist?
So, I tend to write the data literally, as the REPL would print them
(or sometimes to make a subsidiary point with spurious reader macros,
in a form that READ would handle); at the REPL, type:
(read) RET (1 2 3 #3r11 #4r11) RET
Other people prefer to write always evaluable expressions, and they
mean the result of the evaluation of the expression they wrote,
instead of the expression they wrote; at the REPL, type:
(quote (#6r1 #5r2 #4r3 #3r11 #2r101)) RET
> I first thought to a shorthand to apply:
>
> (let ((rest-list '(1 2 3)))
> (apply #'+ rest-list)
>
> or simply:
>
> (+ . rest-list))
>
> but in Scheme it doesn't work.
>
> I don't know if in CL it works or not, so I asked what that point
> was...
Well, strictly speaking, (+ . rest-list) is (denotes) a cons cell
containing the symbol + in its car, and the symbol rest-list in its
cdr. When considered as a form (when we try to evaluate it), it
doesn't mean anything. And considered as data, the substitution isn't
done automatically. So it's just a simplified notation to be
interpreted by a human, or for which you could write a substitution
function:
(let ((rest-list '(1 2 3)))
(subst rest-list 'rest-list '(+ . rest-list))) --> (+ 1 2 3)
(let ((rest-list '(1 2 3)))
(eval (subst rest-list 'rest-list '(+ . rest-list)))) --> 6
--
__Pascal Bourguignon__ http://www.informatimago.com/
"What is this talk of "release"? Klingons do not make software
"releases". Our software "escapes" leaving a bloody trail of
designers and quality assurance people in its wake."
.
- Follow-Ups:
- Re: Hints on recursion
- From: Geoffrey Summerhayes
- Re: Hints on recursion
- References:
- Hints on recursion
- From: zion_zii
- Re: Hints on recursion
- From: Pascal Bourguignon
- Re: Hints on recursion
- From: matteo d'addio 81
- Re: Hints on recursion
- From: Geoffrey Summerhayes
- Re: Hints on recursion
- From: matteo d'addio 81
- Hints on recursion
- Prev by Date: Re: sanity check (coding style)
- Next by Date: Re: Hints on recursion
- Previous by thread: Re: Hints on recursion
- Next by thread: Re: Hints on recursion
- Index(es):
Relevant Pages
|
|