[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: new to lisp (3rd time lucky

William James

5/23/2015 12:45:00 PM

Peter Norvig wrote:

> In my experience, recursions with tests like (if (evenp (first l))) are
> usually not the right way to do it. Instead of thinking about the input
> being a list, and about what the components of the list might be, think
> about all the possible inputs, both list and non-list. Then you get:
>
> * If the input is an even number, then the sum is that number
> * If the input is a list,
> then the sum is the sum of the first plus the sum of the rest
> * Otherwise (either an odd number or anything else) the sum is zero
>
> So you end up with
>
> (defun add-even (x)
> (cond ((and (integerp x) (evenp x)) x)
> ((consp x) (+ (add-even (first x)) (add-even (rest x))))
> (t 0)))

Gauche Scheme:

(define (add-even x)
(if (pair? x)
(apply + (map add-even x))
(if ((every-pred integer? even?) x) x 0)))

(add-even '((2) "2" 3 (4 x (6))))
===>
12

Another way:

(define (add-even x)
(if (pair? x)
(fold (^(y sum) (+ sum (add-even y))) 0 x)
(if ((every-pred integer? even?) x) x 0)))

Another way:

(use srfi-42 :only (sum-ec))
(define (add-even x)
(if (pair? x)
(sum-ec (: y x) (add-even y))
(if ((every-pred integer? even?) x) x 0)))

--
Under the disguise of liberalism, humanism, and democracy Europeans have been
persuaded to commit racial suicide -- a race that has achieved so much and has
survived so much has been tricked into welcoming its own downfall and to take
active measures in order to become a stranger on its own soil.
http://www.kolumbus.fi/aquilon/moscowspee...
1 Answer

William James

1/15/2016 11:49:00 AM

0

WJ wrote:

> Peter Norvig wrote:
>
> > In my experience, recursions with tests like (if (evenp (first l))) are
> > usually not the right way to do it. Instead of thinking about the input
> > being a list, and about what the components of the list might be, think
> > about all the possible inputs, both list and non-list. Then you get:
> >
> > * If the input is an even number, then the sum is that number
> > * If the input is a list,
> > then the sum is the sum of the first plus the sum of the rest
> > * Otherwise (either an odd number or anything else) the sum is zero
> >
> > So you end up with
> >
> > (defun add-even (x)
> > (cond ((and (integerp x) (evenp x)) x)
> > ((consp x) (+ (add-even (first x)) (add-even (rest x))))
> > (t 0)))
>
> Gauche Scheme:
>
> (define (add-even x)
> (if (pair? x)
> (apply + (map add-even x))
> (if ((every-pred integer? even?) x) x 0)))
>
> (add-even '((2) "2" 3 (4 x (6))))
> ===>
> 12
>
> Another way:
>
> (define (add-even x)
> (if (pair? x)
> (fold (^(y sum) (+ sum (add-even y))) 0 x)
> (if ((every-pred integer? even?) x) x 0)))
>
> Another way:
>
> (use srfi-42 :only (sum-ec))
> (define (add-even x)
> (if (pair? x)
> (sum-ec (: y x) (add-even y))
> (if ((every-pred integer? even?) x) x 0)))

MatzLisp (Ruby):

def add_even x
if x.is_a? Array
x.reduce(0){|sum,y| sum + add_even(y)}
elsif x.is_a? Numeric and x.even?
x
else
0
end
end

add_even [[2],3,"hi",4,[[2,6]]]
==>14


--
[A]n unholy alliance of leftists, capitalists, and Zionist supremacists has
schemed to promote immigration and miscegenation with the deliberate aim of
breeding us out of existence in our own homelands.... [T]he real aim stays the
same: the biggest genocide in human history.... --- Nick Griffin
(https://www.youtube.com/watch?v=K...)