Re: difference between a cons pain and plain list

2010-08-17 Thread Alexander Burger
Hi Edwin,

> >   +-+-+ +-+-+
> >   |  a  |  ---+>|  b  | NIL |
> >   +-+-+ +-+-+
> 
> thank you for this. clearer now. this is how it is implemented in
> picolisp underneath, right? (from pico.h)

Yes.

> so if i had a different base data structure, it wouldn't be like this?
> say in other lisp implementations?

AFAIK, this is the same in all Lisp implementations since earliest
times. Perhaps with the exception of "CDR-consing" in some Lisps (an
optimization where the CDR part of a cell is omitted, and a single bit
somewhere indicates that the next cell is the immediately following
memory location).



> reason i'm asking is that if i had arrays as base structures in C
> instead of linked cells, a dotted pair would look something like:
> 
> +-+-+
> |  s  |  t  |
> +-+-+
> 
> and a list as:
> 
> +-+-+-+
> |  s  |  t  | NIL |
> +-+-+-+
> 
> is this a possible way to think about this or am i just chasing windmills?

I think this distinction makes not much sense in C, as there is no
concept of a dotted pair (and also not needed).


In fact, _each_ list consists of dotted pairs, it just happens that the
CDR of each cell in those lists is either another list or NIL. The dot
becomes visible only if the CDR is an atom. Besides that, there's
nothing special about it.

According to Lisp folklore, lists were originally always printed as

   (a . (b . (c . (d . NIL

but due to a bug in the printing routine the list showed up as

   (a b c d)

which was found to be nicer. Don't know if this is true, though.

In any case, dotted pairs are the core of picolisp.

Cheers,
- Alex
-- 
UNSUBSCRIBE: mailto:picol...@software-lab.de?subject=unsubscribe


Re: difference between a cons pain and plain list

2010-08-17 Thread Alexander Burger
On Tue, Aug 17, 2010 at 09:06:56PM +0800, Edwin Eyan Moragas wrote:
> > A three-dimensional coordinate, btw, could be put into a two-cell
> > structure (x y . z), needing only two cells instead of three.
> 
> like so?
> 
> +-+-+ +-+-+
> |  x  |  ---+>|  y  |  z  |
> +-+-+ +-+-+

Yes, exactly.
-- 
UNSUBSCRIBE: mailto:picol...@software-lab.de?subject=unsubscribe


Re: difference between a cons pain and plain list

2010-08-17 Thread Edwin Eyan Moragas
Hi Alex,

On Tue, Aug 17, 2010 at 8:34 PM, Alexander Burger  wro=
te:
>>
>> (a . b ) =3D=3D> [a, b]
>> (a b) =3D=3D> [a, b, NIL]
>
> Well, it depends how your '[' and '[' notation is interpreted. I would
> draw it as cell boxes. (a . b) is a single cell (two pointers), with 'a'
> in the CAR and 'b' in the CDR:
>
> =A0 +-+-+
> =A0 | =A0a =A0| =A0b =A0|
> =A0 +-+-+
>
> while (a b) consists of two cells, where the CDR of the second cell is
> NIL:
>
> =A0 +-+-+ =A0 =A0 +-+-+
> =A0 | =A0a =A0| =A0---+>| =A0b =A0| NIL |
> =A0 +-+-+ =A0 =A0 +-+-+

thank you for this. clearer now. this is how it is implemented in
picolisp underneath, right? (from pico.h)

so if i had a different base data structure, it wouldn't be like this?
say in other lisp implementations?

reason i'm asking is that if i had arrays as base structures in C
instead of linked cells, a dotted pair would look something like:

+-+-+
|  s  |  t  |
+-+-+

and a list as:

+-+-+-+
|   s  |  t  | NIL  |
+-+-+-+

is this a possible way to think about this or am i just chasing windmills?

>
>
>> 3) when is a cons pair appropriate to use?
>
> When you have data structures that typically appear in pairs. A good
> example are two-dimensional coordinates, x and y. You can put a single
> coordinate into a single cell (x . y) instead of a two-element list (x
> y), as you know there will never be a third element.
>
> In that way you save half of the space.
>
> A three-dimensional coordinate, btw, could be put into a two-cell
> structure (x y . z), needing only two cells instead of three.

like so?

+-+-+ +-+-+
|  x  |  ---+>|  y  |  z  |
+-+-+ +-+-+


>
> Besides saving memory, cons pairs also make access easier sometimes. To
> access the 'z' in (x y . z) you can use the function 'cddr', doing two
> pointer dereferences. To access it in (x y z), you need three pointer
> operations (i.e. 'caddr').
>
> Basically, you are free to decide your data structure depending on the
> application. Sometimes a list has advantages over a pair, for example
> when you need uniform access (e.g. 'mapping' over the list).

thank you for taking time to answer my silly questions.

/e
-- 
UNSUBSCRIBE: mailto:picol...@software-lab.de?subject=unsubscribe


Re: difference between a cons pain and plain list

2010-08-17 Thread Alexander Burger
Hi Edwin,

> yes, a lisp newcomer here.

that's a good thing :-)


> looking at the example in the app dev doc:
> 
> : ( '(id . bar) "Title")
> 
> two questions
> 1) if the cons pair was written as (id bar) instead of as
> with the example, would  treat it different? how?

Yes. The function '' calls 'htSytle' internally, which interprets
the style argument (the first argument to most HTML functions). And that
functions interprets styles as certain patterns:

1. If the argument is a single atom, it is taken as a CSS class:

   : ( 'myClass "Title")
   Title

2. If it is a cons pair of two atoms (as in your example above), it is
taken as a single attribute definition:

   : ( '(attr . 12345) "Title") 
   Title

   : ( '(color . red) "Title")  
   Title

3. Otherwise, these rules are applied recursively to the pattern, e.g.

   : ( '(myClass (color . red) (value . 123)) "Title")
   Title

This "style syntax" was chosen arbitrarily, just for practical purposes.


> 2) what's the fundamental difference between a cons pair and a simple
> list? as far as simple symbols go, it would look something like this:
> 
> (a . b ) ==> [a, b]
> (a b) ==> [a, b, NIL]

Well, it depends how your '[' and '[' notation is interpreted. I would
draw it as cell boxes. (a . b) is a single cell (two pointers), with 'a'
in the CAR and 'b' in the CDR:

   +-+-+
   |  a  |  b  |
   +-+-+

while (a b) consists of two cells, where the CDR of the second cell is
NIL:

   +-+-+ +-+-+
   |  a  |  ---+>|  b  | NIL |
   +-+-+ +-+-+


> 3) when is a cons pair appropriate to use?

When you have data structures that typically appear in pairs. A good
example are two-dimensional coordinates, x and y. You can put a single
coordinate into a single cell (x . y) instead of a two-element list (x
y), as you know there will never be a third element.

In that way you save half of the space.

A three-dimensional coordinate, btw, could be put into a two-cell
structure (x y . z), needing only two cells instead of three.

Besides saving memory, cons pairs also make access easier sometimes. To
access the 'z' in (x y . z) you can use the function 'cddr', doing two
pointer dereferences. To access it in (x y z), you need three pointer
operations (i.e. 'caddr').

Basically, you are free to decide your data structure depending on the
application. Sometimes a list has advantages over a pair, for example
when you need uniform access (e.g. 'mapping' over the list).

Cheers,
- Alex
-- 
UNSUBSCRIBE: mailto:picol...@software-lab.de?subject=unsubscribe