[ENH] Lispy list functions
Mayer Goldberg
gmayer at cs.bgu.ac.il
Thu Sep 9 01:25:02 UTC 1999
>If this is the case, and tail does on occassion return a single element,
>then statements like taking the head of a list's tail just don't work.
>(list tail) head (Smalltalk syntax)
>For this to work you must guarantee that tail
>always returns a list. Otherwise you end up sending the
>message 'head' to a single element (which it won't
>understand). My knowledge of Scheme and Lisp is probably
>off here, but with Haskell and Prolog the tail is always
>a collection and not an element. ie:
>#(a b c) tail "should return #(a b)
>#(a b) tail "should return #(b), and not b
>#(b) tail "should return #()
>Any functional programmers out there that can shed some
>light on this?
Here's Quintus Prolog:
| ?- [Head | Tail] = [a | b].
Head = a,
Tail = b ;
no
So I think there's no problem unifying with a non-list. Here's another example:
| ?- [Car | [Cadr | [Caddr | Cdddr]]] = [a, b, c].
Car = a,
Cadr = b,
Caddr = c,
Cdddr = [] ;
no
Prolog behaves just like LISP and Scheme here: The rule for printing a dotted pair (A .
B) is as follows:
- If B = () then (A . B) is printed as (A)
- If B = (C . D) then (A . B) is printed as (A C . D) [and now apply the rule to D].
This means that when the CDR of an ordered pair is a list, one pair of parenthesis and
the dot are not printed. This results in certain ordered pairs looking like lists ... but in
fact the native aggregate data type in LISP and Prolog isn't a list but an ordered pair!
I think the conclusion for this is that for Scheme/LISP/Prolog lists to be represented
correctly in Smalltalk, you really need nested ordered pairs and EmptyList with special
printString methods for both.
Best regards,
Mayer
BTW: I don't know Haskell; It could well be that the CDR must always be a pair in
Haskell. I know there was some drive towards this ... People feel that ordered pairs are
too confusing/complicated to teach. Hence the famous rule in the Little LISPer, that
the second argument to CONS should always be a list, but this isn't a requirement
that's in any official standard of Scheme/LISP/Prolog.
More information about the Squeak-dev
mailing list
|