Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

simple list question (newbie)
In PLT scheme I can do this:

> (define (f l) (car l))
> (f '(1 2 3))
1
> (define (f l) (cdr l))
> (f '(1 2 3))
(2 3)
> (null? '(1 2 3))
#f
> (define (f l) (car l)
(if (not (null? l)) (f (cdr l))))
> (f '(1 2 3))
. car: expects argument of type <pair>; given ()
>


It appears to me that in the first 2 lines nothing
is wrong with my use of car.  However, in the final
definition of f, car is used in the same way.  When
I call the final version of the function f, an error
is generated and the problem seems to be my use of car.


Report this thread to moderator Post Follow-up to this message
Old Post
Chairman of The David Hilbert Appreciation Society
04-10-05 01:57 AM


Re: simple list question (newbie)
Chairman of The David Hilbert Appreciation Society wrote: 
>     (if (not (null? l)) (f (cdr l)))) 
> . car: expects argument of type <pair>; given () 
>
> It appears to me that in the first 2 lines nothing
> is wrong with my use of car.  However, in the final
> definition of f, car is used in the same way.  When
> I call the final version of the function f, an error
> is generated and the problem seems to be my use of car.
>

Run it through trace:

(trace f)
(f '(1 2 3))


[Entering #[compound-procedure 2 f]
Args: (1 2 3)]
[Entering #[compound-procedure 2 f]
Args: (2 3)]
[Entering #[compound-procedure 2 f]
Args: (3)]
[Entering #[compound-procedure 2 f]
Args: ()]
;The object (), passed as the first argument to car, is not the correct
type.

The empty list is not a pair.
--
A. Kanawati
NO.antounk.SPAM@comcast.net

Report this thread to moderator Post Follow-up to this message
Old Post
Antoun Kanawati
04-10-05 08:57 AM


Re: simple list question (newbie)
>  > (define (f l) (car l)
>      (if (not (null? l)) (f (cdr l)))) 
> . car: expects argument of type <pair>; given () 
>
>
> It appears to me that in the first 2 lines nothing
> is wrong with my use of car.  However, in the final
> definition of f, car is used in the same way.  When
> I call the final version of the function f, an error
> is generated and the problem seems to be my use of car.

Try executing

;(define (f l) (car l)
;  (if (not (null? l))
;      (begin
;        (display (cdr l))
;        (newline)
;        (f (cdr l)))))
;
;(f '(1 2 3))

so that you can see what's happening.

The problem is that you are recursively calling f with the current
list's cdr, which eventually leads to (f '()).  This last invocation of
f tries to define (f l) as (car '()), but this is an error because
car's argument must be a pair, and the empty list is not a pair.

The following code avoids the error by testing whether (cdr l) is null
(though it still doesn't really do anything).

;(define (f l) (car l)
;  (if (not (null? (cdr l)))
;      (f (cdr l))))

HTH,

Michael

N.B.  It's conventional to avoid using "l" as a variable name, since it
looks so much like the numeral "1".  Use something like "lst" or "seq"
instead.  (You don't want to use "list", since that would override
Scheme's predefined list function.)

--
Michael D. Hartl, Ph.D.
CTO, Quark Sports LLC
http://quarksports.com/


Report this thread to moderator Post Follow-up to this message
Old Post
Michael Hartl
04-10-05 08:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Scheme archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:11 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.