Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
ACL - Chpater 2, Exercise 9
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  21 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
arnuld  
View profile  
 More options Dec 14 2011, 7:34 am
Newsgroups: comp.lang.lisp
From: arnuld <sunr...@invalid.address>
Date: 14 Dec 2011 12:34:12 GMT
Local: Wed, Dec 14 2011 7:34 am
Subject: ACL - Chpater 2, Exercise 9
EXERCISE STATEMENT: A friend is trying to write a function that returns
the sum of all non-nil elements in the given list. He has written  two
versions of this function, and neither of them works. Explain what is
wrong with each and give a correct version:

1. (defun summit (lst)
    (remove nil lst)
    (apply #'+ lst))

2. (defun summit (lst)
    (let ((x (car lst)))
     (if (null x)
         (summit  (cdr lst))
         (+ x (summit (cdr lst)))))

1st version has problem that (remove) never modifies the original list.
Hence the correct version will be one-liner:  (apply #'+ (remove nil
lst))

2. CLISP says "*** Program Stack Overflow . RESET" . Oh.. let me see,  
Recursion never bottoms out and hence it hits the stack limit. But then
how to write a correct version which take care of nil atoms and bottoms
out too ?

IDEA: rather than setting x to (car lst) and checking for null, we can
keep on doing cdr till the end and when we will hit nil it means we have
reached the last atom of list, from there onwards we can add values, tail-
recursion. I am unable to implement it though

Is it correct way ?

--
 arnuld
http://LispMachine.Wordpress.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank DG1SBG  
View profile  
 More options Dec 14 2011, 8:29 am
Newsgroups: comp.lang.lisp
From: Frank DG1SBG <dg1...@googlemail.com>
Date: Wed, 14 Dec 2011 14:29:24 +0100
Local: Wed, Dec 14 2011 8:29 am
Subject: Re: ACL - Chpater 2, Exercise 9

I'll answer this by showing another version of #'summit:

(defun summit (list)
  (apply #'+ (remove nil list)))

Q: Which of the problems you stated are solved by this version?

This version still has some disadvantages which the following version
avoids:

(defun summit (list)
  (apply #'+ (remove-if-not #'numberp list)))

Q: Which disadvantages have been cleared by this version ?

Q: Which problems are still present in this last version ?

Hth!

Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tamas Papp  
View profile  
 More options Dec 14 2011, 8:46 am
Newsgroups: comp.lang.lisp
From: Tamas Papp <tkp...@gmail.com>
Date: Wed, 14 Dec 2011 13:46:47 +0000 (UTC)
Local: Wed, Dec 14 2011 8:46 am
Subject: Re: ACL - Chpater 2, Exercise 9

I don't know what correct means in this context, but perhaps a more
idiomatic way would be to use REDUCE, treating NIL as 0.  Eg

(defun summit (sequence)
  "Sum non-nil elements of SEQUENCE."
  (reduce #'+ sequence :key (lambda (element)
                              (if (null element)
                                  0
                                  element))))

(summit '(1 2 3)) ; 6
(summit '(1 nil 2 3)) ; 6

This has the extra advantage that it works for all kinds of sequences:

(summit #(1 nil 2 3)) ; 6

Graham's ACL follows the pedagogical tradition of emphasizing
recursion, especially tail recursion.  It is certainly valuable to
learn about these things, and recursion can be a very nice idiom for
some problems, but I wonder if you would be better off learning from
Seibel's Practical Common Lisp book (you can find it on the net),
which (IMO) teaches a more idiomatic Common Lisp style.

Best,

Tamas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Don Geddis  
View profile  
 More options Dec 14 2011, 11:29 am
Newsgroups: comp.lang.lisp
From: Don Geddis <d...@geddis.org>
Date: Wed, 14 Dec 2011 08:29:34 -0800
Local: Wed, Dec 14 2011 11:29 am
Subject: Re: ACL - Chpater 2, Exercise 9
arnuld <sunr...@invalid.address> wrote on 14 Dec 2011 12:3:

> EXERCISE STATEMENT: A friend is trying to write a function that returns
> the sum of all non-nil elements in the given list.
> 2. (defun summit (lst)
>     (let ((x (car lst)))
>      (if (null x)
>          (summit  (cdr lst))
>          (+ x (summit (cdr lst)))))
> 2. CLISP says "*** Program Stack Overflow . RESET" . Oh.. let me see,  
> Recursion never bottoms out and hence it hits the stack limit. But then
> how to write a correct version which take care of nil atoms and bottoms
> out too ?

Your problem is that at the very end of the list, (CDR LST) = NIL, so
SUMMIT gets called again with (SUMMIT NIL).  Which, in your version,
recurses again.  (Note the (CDR NIL) also = NIL, forever.)

So what you need is a check for this final special case:

        (defun summit (lst)
          (if (null lst)
              0
              ... ))

> IDEA: rather than setting x to (car lst) and checking for null, we can
> keep on doing cdr till the end and when we will hit nil it means we have
> reached the last atom of list, from there onwards we can add values, tail-
> recursion. I am unable to implement it though
> Is it correct way ?

You need both.  The check for (NULL LST) is to see if you're at the end
of the recursion.  The check for (NULL X) is to see whether the current
element is a number (in which case you add it to the sum), or else a NIL
(in which case you ignore it).

Speaking of which, (NOT (NUMBERP X)) might be a better test, in your
original function, than the (NULL X) you have there...

        -- Don
___________________________________________________________________________ ____
Don Geddis                  http://don.geddis.org/               d...@geddis.org
Happiness is having a chitinous exoskeleton.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Dec 14 2011, 12:53 pm
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@alum.mit.edu>
Date: Wed, 14 Dec 2011 12:53:19 -0500
Local: Wed, Dec 14 2011 12:53 pm
Subject: Re: ACL - Chpater 2, Exercise 9
In article <4ee897c4$0$288$14726...@news.sunsite.dk>,

Correct.

> 2. CLISP says "*** Program Stack Overflow . RESET" . Oh.. let me see,  
> Recursion never bottoms out and hence it hits the stack limit. But then
> how to write a correct version which take care of nil atoms and bottoms
> out too ?

What should be the result if it gets an empty list?  That's the "bottom
out" case.

> IDEA: rather than setting x to (car lst) and checking for null, we can
> keep on doing cdr till the end and when we will hit nil it means we have
> reached the last atom of list, from there onwards we can add values, tail-
> recursion. I am unable to implement it though

> Is it correct way ?

That's one way, but it's not the simple way.  The code above is almost
correct, it's just missing the bottom case.  Add that and it should work
fine.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
arnuld  
View profile  
 More options Dec 16 2011, 8:49 am
Newsgroups: comp.lang.lisp
From: arnuld <sunr...@invalid.address>
Date: 16 Dec 2011 13:49:04 GMT
Local: Fri, Dec 16 2011 8:49 am
Subject: Re: ACL - Chpater 2, Exercise 9

That seems more like production/industrial/real-life code as compared to
ACL exercise which looks more like  academic code to me.

> This has the extra advantage that it works for all kinds of sequences:

> (summit #(1 nil 2 3)) ; 6
> Graham's ACL follows the pedagogical tradition of emphasizing recursion,
> especially tail recursion.  It is certainly valuable to learn about
> these things, and recursion can be a very nice idiom for some problems,
> but I wonder if you would be better off learning from Seibel's Practical
> Common Lisp book (you can find it on the net), which (IMO) teaches a
> more idiomatic Common Lisp style.

I thought recursion was CL's basic paradigm (ACL says its functional
programming). I searched a lot on CLL archives but I couldn't find anyone
claiming that PCL teaches more idiomatic Lidp style as compared to ACL.

Myself, as I have finished 2 chapters of ACL, I found it little but
academic and at the same time quite terse.

--
 arnuld
http://LispMachine.Wordpress.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jussi Piitulainen  
View profile  
 More options Dec 16 2011, 9:22 am
Newsgroups: comp.lang.lisp
From: Jussi Piitulainen <jpiit...@ling.helsinki.fi>
Date: 16 Dec 2011 16:22:59 +0200
Local: Fri, Dec 16 2011 9:22 am
Subject: Re: ACL - Chpater 2, Exercise 9

Nothing un-academic in it.

If you want to know functional programming, you want to be able not
only to use reduce, sometimes called fold, but also to define similar
"higher order" functions for yourself.

They package patterns of recursion in a reusable form. It's called
abstraction. It's good.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zach Beane  
View profile  
 More options Dec 16 2011, 9:25 am
Newsgroups: comp.lang.lisp
From: Zach Beane <x...@xach.com>
Date: Fri, 16 Dec 2011 09:25:13 -0500
Local: Fri, Dec 16 2011 9:25 am
Subject: Re: ACL - Chpater 2, Exercise 9

arnuld <sunr...@invalid.address> writes:
> I thought recursion was CL's basic paradigm (ACL says its functional
> programming). I searched a lot on CLL archives but I couldn't find anyone
> claiming that PCL teaches more idiomatic Lidp style as compared to ACL.

I will claim that. PCL is about Common Lisp, and ANSI Common Lisp is
about a Platonic Lisp, (reluctantly) using Common Lisp as the
implementation language. Paul Graham is not a fan of Common Lisp, and I
think it shows in ACL.

Zach


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Bradshaw  
View profile  
 More options Dec 16 2011, 10:34 am
Newsgroups: comp.lang.lisp
From: Tim Bradshaw <t...@tfeb.org>
Date: Fri, 16 Dec 2011 15:34:06 +0000
Local: Fri, Dec 16 2011 10:34 am
Subject: Re: ACL - Chpater 2, Exercise 9
On 2011-12-16 14:25:13 +0000, Zach Beane said:

> I will claim that. PCL is about Common Lisp, and ANSI Common Lisp is
> about a Platonic Lisp, (reluctantly) using Common Lisp as the
> implementation language. Paul Graham is not a fan of Common Lisp, and I
> think it shows in ACL.

I agree with that, however I do think ACL is a lovely book to read in
the way SICP or AMOP or K&R are lovely books to read, for instance (I
don't want to make some claim of which one is better or anything
silly): there's a lot of understanding to be had from them.  PCL may be
the same: I have not read it so carefully, though it did not make the
impression on me that ACL did, which may solely be because I'm more
jaded now of course, in any case please don't take this as implying I
don't like PCL or think ACL is better or anything.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Mizrahi  
View profile  
 More options Dec 16 2011, 12:16 pm
Newsgroups: comp.lang.lisp
From: Alex Mizrahi <alex.mizr...@gmail.com>
Date: Fri, 16 Dec 2011 19:16:28 +0200
Local: Fri, Dec 16 2011 12:16 pm
Subject: Re: ACL - Chpater 2, Exercise 9

> 2. CLISP says "*** Program Stack Overflow . RESET" . Oh.. let me see,
> Recursion never bottoms out and hence it hits the stack limit. But then
> how to write a correct version which take care of nil atoms and bottoms
> out too ?
> IDEA: rather than setting x to (car lst) and checking for null, we can
> keep on doing cdr till the end and when we will hit nil it means we have
> reached the last atom of list, from there onwards we can add values, tail-
> recursion. I am unable to implement it though
> Is it correct way ?

No. You need TWO tests here:
    one for CAR (contents of list)
    another for CDR (list structure)

This would give you four cases to implement, but you can simplify this
by checking whether list itself is empty, then there are only three cases.

In general, I think you should have picked an idiom of checking empty
list case first.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Mizrahi  
View profile  
 More options Dec 16 2011, 12:39 pm
Newsgroups: comp.lang.lisp
From: Alex Mizrahi <alex.mizr...@gmail.com>
Date: Fri, 16 Dec 2011 19:39:21 +0200
Local: Fri, Dec 16 2011 12:39 pm
Subject: Re: ACL - Chpater 2, Exercise 9

> That seems more like production/industrial/real-life code as compared to
> ACL exercise which looks more like  academic code to me.

Arguably even more industrial:

(loop for x in list
       when x
       summing x)

> I thought recursion was CL's basic paradigm

Recursion is widely used to implement list processing algorithms because
it's quite natural for this task. List processing, however, is a tiny
part of CL, and CL isn't better at recursion than pretty much any other
language. But it's worse than functional languages like Scheme and
Haskell because they guarantee tail-call optimization while CL does not.

So it's not a 'CL's basic paradigm'.

> (ACL says its functional programming).

Modern functional programming is mostly about higher-order functions.
CL is somewhat better than, say, C as it supports lexical closures, but
it doesn't provide much more features than, say, Python or JavaScript.

If you're into functional programming it's better to check Scheme (SICP)
and Haskell. Maybe something like Ocaml.

CL which differentiate CL from other languages are CLOS, macros,
non-local control transfers with signal, restart etc.

> I searched a lot on CLL archives but I couldn't find anyone
> claiming that PCL teaches more idiomatic Lidp style as compared to ACL.

It's hard to say what is idiomatic Lisp style because it's a freaking
old language and there are many styles. But PCL definitely shows you
more of a modern CL style just because it's written much later and it's
not a textbook but a book about writing practical applications.

You can also find some good CL style in PAIP -- it's rather old, but
Peter Norvig is a freaking genius.

Also check some good modern libraries.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kaz Kylheku  
View profile  
 More options Dec 16 2011, 1:09 pm
Newsgroups: comp.lang.lisp
From: Kaz Kylheku <k...@kylheku.com>
Date: Fri, 16 Dec 2011 18:09:16 +0000 (UTC)
Local: Fri, Dec 16 2011 1:09 pm
Subject: Re: ACL - Chpater 2, Exercise 9
On 2011-12-16, arnuld <sunr...@invalid.address> wrote:

>> On Wed, 14 Dec 2011 13:46:47 +0000, Tamas Papp wrote:
>> This [reduce-based summit function] has the extra advantage that it works for
>> all kinds of sequences:

>> (summit #(1 nil 2 3)) ; 6

>> Graham's ACL follows the pedagogical tradition of emphasizing recursion,
>> especially tail recursion.  It is certainly valuable to learn about
>> these things, and recursion can be a very nice idiom for some problems,
>> but I wonder if you would be better off learning from Seibel's Practical
>> Common Lisp book (you can find it on the net), which (IMO) teaches a
>> more idiomatic Common Lisp style.

> I thought recursion was CL's basic paradigm (ACL says its functional
> programming).

Note that REDUCE is still purely functional, and it appears in purely
functional languges, under different names like "fold", "left-fold",
"fold-left", "inject", etc, or denoted by some cryptic operator glyph.

It might not be implemented recursively, but you can't tell.

You don't have to write explicit recursion to do functional programming.
Recursion over basic building blocks like CAR and CDR is very low-level
functional programming.

In Common Lisp, you can throw away functional purity if it's inconvenient or
inefficient in the implementation, yet write a module which has a functional
API.

Uses of loop are not necessarily imperative. For instance:

  (loop for x below 10 summing x)

This is purely functional, to me.  The imperative-ness will reveal itself if
you create lexical closures in the loop body.

  ;; collect 10 functions that return x, then pass
  ;; through mapcar #'funcall to call them all

  (mapcar #'funcall (loop for x below 10 collecting (lambda () x)))

  -> (10 10 10 10 10 10 10 10 10 10)

Oops! This tells us something we already know: that there is actually only one
X. We made ten lexical closures in different iterations of the loop, but they
all captured the same X, which means that loop works by instantiating a single
variable X and incrementing it.

However, it's not obvious in many uses of loop.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Raffael Cavallaro  
View profile  
 More options Dec 17 2011, 10:33 am
Newsgroups: comp.lang.lisp
From: Raffael Cavallaro <raffaelcavall...@pas.despam.s.il.vous.plait.mac.com>
Date: Sat, 17 Dec 2011 10:33:23 -0500
Local: Sat, Dec 17 2011 10:33 am
Subject: Re: ACL - Chpater 2, Exercise 9
On 2011-12-16 15:34:06 +0000, Tim Bradshaw said:

> I do think ACL is a lovely book to read in the way SICP or AMOP or K&R
> are lovely books to read

Agreed entirely. I found it very enjoyable in exactly the same way. I
wouldn't recommend it as a tutorial to anyone who didn't already have a
good grounding in programming - I read it after reading SICP and K&R
for example.

For a beginner's intro to the lisp family of languages it's hard to
beat _The Little Schemer_ & _The Seasoned Schemer_ imho. For a
practical, hands-on, nuts and bolts approach to *common lisp* PCL is
excellent. For something a bit more entertaining and engaging, _Land of
Lisp_ is great.

warmest regards,

Ralph

--
Raffael Cavallaro


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
WJ  
View profile  
 More options Feb 17 2012, 1:56 am
Newsgroups: comp.lang.lisp
From: "WJ" <w_a_x_...@yahoo.com>
Date: 17 Feb 2012 06:56:52 GMT
Local: Fri, Feb 17 2012 1:56 am
Subject: Re: ACL - Chpater 2, Exercise 9

MatzLisp:

summit = lambda{|list| list.compact.reduce( :+ )}

summit[ [1,nil,3,5,7,9,11] ]

  ===> 36


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kaz Kylheku  
View profile  
 More options Feb 17 2012, 4:16 am
Newsgroups: comp.lang.lisp
From: Kaz Kylheku <k...@kylheku.com>
Date: Fri, 17 Feb 2012 09:16:55 +0000 (UTC)
Local: Fri, Feb 17 2012 4:16 am
Subject: Re: ACL - Chpater 2, Exercise 9
On 2012-02-17, WJ <w_a_x_...@yahoo.com> wrote:

> MatzLisp:

> summit = lambda{|list| list.compact.reduce( :+ )}

> summit[ [1,nil,3,5,7,9,11] ]

TXR Lisp:

[apply + (flatten '(1 nil 3 5 7 9 11))]

Ruby is designed by fucking imbeciles.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
tar...@google.com  
View profile  
 More options Feb 17 2012, 1:50 pm
Newsgroups: comp.lang.lisp
From: tar...@google.com
Date: Fri, 17 Feb 2012 10:50:17 -0800 (PST)
Local: Fri, Feb 17 2012 1:50 pm
Subject: Re: ACL - Chpater 2, Exercise 9
Agreed.
I think for ease of seeing the algorithm and incorporating all of the suggestions, a good version would be

(defun summit2 (list)
  (cond ((null list) 0)
        ((numberp (car list)) (+ (car list) (summit2 (cdr list))))
        (t (summit2 (cdr list)))))

(summit2 '(1 2 nil 3 a 4))  ==> 10

Disclaimer:
This version does repeat the (car list) form, but it makes the intent clearer. Besides, a reasonable compiler should be able to handle the common expression. In the worst case, CAR is always pretty fast...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Bradshaw  
View profile  
 More options Feb 18 2012, 8:57 am
Newsgroups: comp.lang.lisp
From: Tim Bradshaw <t...@tfeb.org>
Date: Sat, 18 Feb 2012 13:57:24 +0000
Local: Sat, Feb 18 2012 8:57 am
Subject: Re: ACL - Chpater 2, Exercise 9
On 2012-02-17 18:50:17 +0000, tar...@google.com said:

> (defun summit2 (list)
>   (cond ((null list) 0)
>         ((numberp (car list)) (+ (car list) (summit2 (cdr list))))
>         (t (summit2 (cdr list)))))

> (summit2 '(1 2 nil 3 a 4))  ==> 10

> Disclaimer:
> This version does repeat the (car list) form, but it makes the intent
> clearer. Besides, a reasonable compiler should be able to handle the
> common expression. In the worst case, CAR is always pretty fast...

That's not it's only problem.

(defun summit3 (l)
  (labels ((s3a (ll a)
             (cond ((null ll)
                    a)
                   ((numberp (first l))
                    (s3a (rest ll) (+ a (first ll))))
                   (t
                    (s3a (rest ll) a)))))
    (s3a l 0)))

Or, if you want to go mad

(defun summit4 (l)
  (labels ((s4a (ll a)
             (if (null ll)
                 a
               (destructuring-bind (f . r) ll
                 (s4a r (if (numberp f) (+ a f) a))))))
    (s4a l 0)))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Bradshaw  
View profile  
 More options Feb 18 2012, 8:58 am
Newsgroups: comp.lang.lisp
From: Tim Bradshaw <t...@tfeb.org>
Date: Sat, 18 Feb 2012 13:58:04 +0000
Local: Sat, Feb 18 2012 8:58 am
Subject: Re: ACL - Chpater 2, Exercise 9
On 2012-02-18 13:57:24 +0000, Tim Bradshaw said:

> That's not it's only problem.

Argh.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
WJ  
View profile  
 More options Mar 6 2012, 4:53 am
Newsgroups: comp.lang.lisp
From: "WJ" <w_a_x_...@yahoo.com>
Date: 6 Mar 2012 09:53:50 GMT
Local: Tues, Mar 6 2012 4:53 am
Subject: Re: ACL - Chpater 2, Exercise 9

NewLisp:

(define (summit lst)
  (apply + (replace nil lst)))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
WJ  
View profile  
 More options Mar 24 2012, 12:31 am
Newsgroups: comp.lang.lisp
From: "WJ" <w_a_x_...@yahoo.com>
Date: 24 Mar 2012 04:31:53 GMT
Local: Sat, Mar 24 2012 12:31 am
Subject: Re: ACL - Chpater 2, Exercise 9

Clojure:

user=> (reduce #(if %2 (+ %1 %2) %1) 0 '(1 nil 2 3))
6


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
WJ  
View profile  
 More options Apr 26 2012, 2:05 am
Newsgroups: comp.lang.lisp
From: "WJ" <w_a_x_...@yahoo.com>
Date: 26 Apr 2012 06:05:37 GMT
Local: Thurs, Apr 26 2012 2:05 am
Subject: Re: ACL - Chpater 2, Exercise 9

Racket:

(for/sum ([i '(1 nil 2 3)] #:when (number? i)) i) ; 6

(for/sum ([i #(1 nil 2 3)] #:when (number? i)) i) ; 6


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »