Hope someone can give me a clue.
I am new to lisp programming, I have to write a very small
recursive function to do the following:
it gets a list passed and an integer, it will trim n items
off the front of the list
>(TRIM'(A B C D E F) 2)
(C D E F)
this is what i wrote
(defun trim(l n)
(cond ((null l) nil )
(T (trim (rest l)(- n 1)))
(if (= n 0) l)
)
)
and the result
>(trim '(a b c d f)2)
NIL
So what is it that im doing wrong?
am i totally on the wrong track? I've been using lisp for about two
weeks, and
this is part of an assignment.
thanks for any feedback
Natalia
Natalia <····@one.net.au> schrieb in im Newsbeitrag:
·················@one.net.au...
> Hope someone can give me a clue.
> I am new to lisp programming, I have to write a very small
> recursive function to do the following:
> it gets a list passed and an integer, it will trim n items
> off the front of the list
> >(TRIM'(A B C D E F) 2)
> (C D E F)
>
> this is what i wrote
>
> (defun trim(l n)
> (cond ((null l) nil )
> (T (trim (rest l)(- n 1)))
> (if (= n 0) l)
> )
> )
>
> and the result
> >(trim '(a b c d f)2)
> NIL
You program is basically o.k., but of course the last clause will never be
executed and th if is obsolete, you could use instead
(defun trim(l n)
(cond ((null l) nil )
((= n 0) l)
(T (trim (rest l)(- n 1)))))
(Though I must warn you: I am not a lisp expert!)
Janos Blazi
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
> You program is basically o.k., but of course the last clause will never be
> executed and th if is obsolete, you could use instead
>
> (defun trim(l n)
> (cond ((null l) nil )
> ((= n 0) l)
> (T (trim (rest l)(- n 1)))))
>
> (Though I must warn you: I am not a lisp expert!)
>
> Janos Blazi
Here is another version:
(defun trim1 (l n)
(if (or (null l) (= n 0)) l
(trim (rest l)(- n 1))))
Janos Blazi
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
Janos Blazi <······@netsurf.de> schrieb in im Newsbeitrag:
··········@127.0.0.1...
> > You program is basically o.k., but of course the last clause will never
be
> > executed and th if is obsolete, you could use instead
> >
> > (defun trim(l n)
> > (cond ((null l) nil )
> > ((= n 0) l)
> > (T (trim (rest l)(- n 1)))))
> >
> > (Though I must warn you: I am not a lisp expert!)
> >
> > Janos Blazi
>
> Here is another version:
>
> (defun trim1 (l n)
> (if (or (null l) (= n 0)) l
> (trim (rest l)(- n 1)))) /**** SORRY, BUG: CORRECT IS (trim1 ...)
>
> Janos Blazi
>
>
>
>
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
Natalia <····@one.net.au> writes:
> Hope someone can give me a clue.
> I am new to lisp programming, I have to write a very small
> recursive function to do the following:
> it gets a list passed and an integer, it will trim n items
> off the front of the list
> >(TRIM'(A B C D E F) 2)
> (C D E F)
The snappy answer would be to use nthcdr and be done with it, but
professors are funny...
> this is what i wrote
>
> (defun trim(l n)
> (cond ((null l) nil )
> (T (trim (rest l)(- n 1)))
> (if (= n 0) l)
> )
> )
Reformatted, with better variable names:
(defun trim (the-list elem-count)
(cond ((null the-list) nil )
(T (trim (rest the-list)(- elem-count 1)))
(if (= elem-count 0) the-list)))
When I compile this, my Lisp says
Warning: This variable is undefined:
IF
so there's clearly something wrong.
First thing you ought to do is trace your function; type
(trace trim)
in your lisp listener and then call your function. You will see the
recursive calls and the arguments that they get passed; their values
might surprise you ;-)
> So what is it that im doing wrong?
> am i totally on the wrong track? I've been using lisp for about two
> weeks, and
> this is part of an assignment.
My guess is that you try to remodel an example program here that
recurses until a list is empty, but your program ought to call itself
until something becomes zero; you have to rethink this (cond ...)
statement in the code a bit.
> thanks for any feedback
> Natalia
You're welcome.
Rudolf Schlatte wrote:
>
> The snappy answer would be to use nthcdr and be done with it, but
> professors are funny...
>
yeah this is one of the requirements that we are not to use the nth
function
or any of its relatives.
> When I compile this, my Lisp says
> Warning: This variable is undefined:
> IF
> so there's clearly something wrong.
>
my compiler accepts it, but i will still use trace and see what happens,
> My guess is that you try to remodel an example program here that
> recurses until a list is empty, but your program ought to call itself
> until something becomes zero; you have to rethink this (cond ...)
> statement in the code a bit.
thank you very much for your feedback.
will see what happens
Natalia :)