[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Interpolating into a list

user

7/29/2015 11:59:00 PM

Hi. I'm trying to learn Common Lisp and I've come across something I
just don't understand. I have been trying to write a function that
takes an initial list of numbers and repeatedly calculates an
interpolated value between each pair of adjacent members, then places the
interpolation into the list between the members it was formed from. But
the list I end up with seems weird. I've boiled down the issue to the
following sample of code

(do ((l1 '(1 2 3))
flag
)
(flag
(format t "~a ~a ~a ~a~%" l1 (car l1) (cdr l1) (length l1))
)
(do ((l2 l1 (cddr l2)))
((null l2))
(rplacd l2 (cons 4 (cdr l2)))
)
(setq flag t)
)

When I run this the 'format' output is

(1 4 2 4 3 4) 1 (2 4 3 4) 3

I.e. I apparently end up with a list that seems to contain six members,
but for which 'length' returns 3.

Grateful for any elucidation. I'm running Steel Bank on Linux.

Ron
4 Answers

Pascal J. Bourguignon

7/30/2015 12:14:00 AM

0

user <ron@ronmitchell.co.uk> writes:

> Hi. I'm trying to learn Common Lisp and I've come across something I
> just don't understand. I have been trying to write a function that
> takes an initial list of numbers and repeatedly calculates an
> interpolated value between each pair of adjacent members, then places the
> interpolation into the list between the members it was formed from. But
> the list I end up with seems weird. I've boiled down the issue to the
> following sample of code
>
> (do ((l1 '(1 2 3))
> flag
> )
> (flag
> (format t "~a ~a ~a ~a~%" l1 (car l1) (cdr l1) (length l1))
> )
> (do ((l2 l1 (cddr l2)))
> ((null l2))
> (rplacd l2 (cons 4 (cdr l2)))
> )
> (setq flag t)
> )
>
> When I run this the 'format' output is
>
> (1 4 2 4 3 4) 1 (2 4 3 4) 3
>
> I.e. I apparently end up with a list that seems to contain six members,
> but for which 'length' returns 3.
>
> Grateful for any elucidation. I'm running Steel Bank on Linux.

Let's consider your program.

Remember, lisp source are s-exp, S-expressions, which are lisp data
structures, notably "lists", that is, chains of cons cells.

Here is a representation of your lisp source:

cl-user> (com.informatimago.common-lisp.picture.cons-to-ascii:draw-list
'(do ((l1 '(1 2 3))
flag)
â?¦))

+-----------------------------------------------------------------------+
| (do ((l1 '(1 2 3)) flag) â?¦) |
| |
| +---+---+ +---+---+ +---+---+ |
| | * | * |-->| * | * |-->| * |NIL| |
| +---+---+ +---+---+ +---+---+ |
| | | | |
| v | v |
| +----+ | +---+ |
| | do | | | â?¦ | |
| +----+ | +---+ |
| v |
| +---+---+ +---+---+ |
| | * | * |-->| * |NIL| |
| +---+---+ +---+---+ |
| | | |
| | v |
| | +------+ |
| | | flag | |
| | +------+ |
| v |
| +---+---+ +---+---+ |
| | * | * |-->| * |NIL| |
| +---+---+ +---+---+ |
| | | |
| v v |
| +----+ +---+---+ +---+---+ |
| | l1 | | * | * |-->| * |NIL| |
| +----+ +---+---+ +---+---+ |
| | | |
| v v |
| +-------+ +---+---+ +---+---+ +---+---+ |
| | quote | | * | * |-->| * | * |-->| * |NIL| |
| +-------+ +---+---+ +---+---+ +---+---+ |
| | | | |
| v v v |
| +---+ +---+ +---+ |
| | 1 | | 2 | | 3 | |
| +---+ +---+ +---+ |
+-----------------------------------------------------------------------+

What do you think this (quote (1 2 3)) operator will return?


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

user

7/30/2015 4:48:00 PM

0

Hi Pascal, thanks for the reply.

Well, if I type (quote (1 2 3)) into the SBCL repl it comes back with (1 2 3).

In terms of your code diagram this would be

|
v
+---+---+ +---+---+ +---+---+
| * | * |-->| * | * |-->| * |NIL|
+---+---+ +---+---+ +---+---+
| | |
v v v
+---+ +---+ +---+
| 1 | | 2 | | 3 |
+---+ +---+ +---+

where the result is indicated by the top 'arrow' in the diagram.

Ah, I think I might be beginning to see... Is the problem something to do with the fact that I am actually modifying the 'do' code itself?

Ron

Barry Margolin

7/30/2015 7:05:00 PM

0

In article <d10a6ff9-7d26-4088-a267-ce4bdee47224@googlegroups.com>,
ron@ronmitchell.co.uk wrote:

> Hi Pascal, thanks for the reply.
>
> Well, if I type (quote (1 2 3)) into the SBCL repl it comes back with (1 2
> 3).
>
> In terms of your code diagram this would be
>
> |
> v
> +---+---+ +---+---+ +---+---+
> | * | * |-->| * | * |-->| * |NIL|
> +---+---+ +---+---+ +---+---+
> | | |
> v v v
> +---+ +---+ +---+
> | 1 | | 2 | | 3 |
> +---+ +---+ +---+
>
> where the result is indicated by the top 'arrow' in the diagram.
>
> Ah, I think I might be beginning to see... Is the problem something to do
> with the fact that I am actually modifying the 'do' code itself?

Exactly. You're calling RPLACD on a literal list, so it's modifying the
list that's in the function definition.

Change your original function to use

(do ((l1 (list 1 2 3))

and the problem will go away.

P.S. you should have replied to Pascal's post, not your own original
post, so that the threading would be maintained properly. Usenet is not
a web forum (despite the fact that you're reading it in the forum-style
Google Groups), you can't treat it like it is.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

user

7/30/2015 9:01:00 PM

0

Thanks Barry, that helps a lot.

Sorry about the mis-reply. Google Groups is the only way I could find
to access comp.lang.lisp in my comprehensively firewalled office, and I
hit the wrong button. Now trying Gnus and hope I get it right this
time.