Hi Edwin, > yes, a lisp newcomer here.
that's a good thing :-) > looking at the example in the app dev doc: > > : (<h1> '(id . bar) "Title") > > two questions > 1) if the cons pair was written as <code>(id bar)</code> instead of as > with the example, would <h1> treat it different? how? Yes. The function '<h1>' 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: : (<h1> 'myClass "Title") <h1 class="myClass">Title</h1> 2. If it is a cons pair of two atoms (as in your example above), it is taken as a single attribute definition: : (<h1> '(attr . 12345) "Title") <h1 attr="12345">Title</h1> : (<h1> '(color . red) "Title") <h1 color="red">Title</h1> 3. Otherwise, these rules are applied recursively to the pattern, e.g. : (<h1> '(myClass (color . red) (value . 123)) "Title") <h1 class="myClass" color="red" value="123">Title</h1> 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:[email protected]?subject=unsubscribe
