[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: the little schemer

William James

4/9/2015 7:31:00 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)
2 Answers

Jeff Barnett

4/10/2015 2:13:00 AM

0

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

Kaz Kylheku

4/10/2015 3:34:00 AM

0

On 2015-04-10, Jeff Barnett <jbb@notatt.com> wrote:
> 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.

() is a list in Scheme, too.

It's an empty list and so it doesn't have first element. The goal is to extract
and collect the first elements from those lists which have elements.

This is equally sensible in Common Lisp and Scheme.