Jeff Barnett
4/10/2015 2:13:00 AM
WJ wrote on 4/9/2015 1:30 AM:
> R. Mattes wrote:
>
>>>> I am working through The Little Schemer - translating their examples
>>>> from the scheme dialect to Common Lisp (Lisp In A Box with the CLisp
>>>> module).
>>>>
>>>> I am stuck on the following function called 'firsts'. It takes a
>>>> non-null list as an argument and returns the first argument of each top
>>>> level internal list.
>>>>
>>>> firsts applied to the list ((a b) (c d) (e f)) should return (a c e)
>>>
>>> (defun firsts (list)
>>> (mapcar #'first list))
>>>
>>
>> This of course only works if _all_ elements of list are cons which
>> is not guaranteed by the original request.
>
> Gauche Scheme and Racket:
>
> (define (firsts list)
> (filter-map
> (lambda (x) (and (pair? x) (car x)))
> list))
>
> (firsts '(() a (b 2) (c 3) d ()))
>
> ===>
> (b c)
I don't think this is correct. Nil is a list in CL. So, pick your
poison! Further, isn't the ponderous (and (pair? x) (car x)) the same as
(car x)?
--
Jeff Barnett