[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: help on lisp project

William James

5/9/2015 1:27:00 AM

> Can anyone help a beginner lisp programmer?
>
> I want to define a function: EVERY-OTHER-NUMBER that takes a list as
> argument and returns a list with every other number from the original
> list. For example:
>
> (EVERY-OTHER-NUMBER '(this 5 is really 2 insane 3 41 28))
>
> should return this list (2 41). If there is less than two number in a
> list, return nil. I also have to use recursion. Do I need
> another function that calls EVERY-OTHER-NUMBER that does part of the work?
>
> The built-in functions that I can only use are:
>
> first, rest, cons, append, list, if, cond, length,
> defun, let, null, atom, listp, numberp, zerop, equal,
> eq, symbolp, and, or, not, +, -, 1-, *, /, =, >, <, setq

Gauche Scheme:

(define (every-other-number lst)
(let go ((lst lst) (ok #f))
(if (null? lst)
'()
(if (number? (car lst))
(if ok
(cons (car lst) (go (cdr lst) #f))
(go (cdr lst) #t))
(go (cdr lst) ok)))))


(every-other-number '(this 5 is really 2 insane 3 41 28))
===>
(2 41)

--
"Sverigedemokraterna", a party outside the Parliament, can seldom hold meetings
without being hassled by political hooligans, who make noise, destroy equipment
or even resort to violence. There are no reactions to this in mass media, nor
from the police.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html