[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

A function to return the sum of the numbers less than n

gengyangcai

11/8/2015 9:54:00 AM

This is a function to return the sum of the numbers less than n. ----- (1)

(defun sum (n) ------- (A)
(let ((s 0)) ------- (B)
(dotimes (i n s) ------- (C)
(incf si)))) -------- (D)

It gives this output when I run the code in LispWorks : SUM

Line A : Defun defines function. Sum is the function. n is simply n as defined in (1)

What about lines B, C and D ? What do the terms like let , s , 0 , dotimes , i , n , s , inc. and si mean ?
17 Answers

Pascal J. Bourguignon

11/8/2015 10:25:00 AM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> This is a function to return the sum of the numbers less than n. ----- (1)
>
> (defun sum (n) ------- (A)
> (let ((s 0)) ------- (B)
> (dotimes (i n s) ------- (C)
> (incf si)))) -------- (D)
>
> It gives this output when I run the code in LispWorks : SUM
>
> Line A : Defun defines function. Sum is the function. n is simply n as
> defined in (1)
>
> What about lines B, C and D ? What do the terms like let , s , 0 ,
> dotimes , i , n , s , inc. and si mean ?

Aw! I thought you switched to python. :-(

First, there's a bug in this function: the variable SI is undefined.
Also, the variable I is unused. And there's no term INC.

Obviously, it wanted to be:

(defun sum (n)
(let ((s 0))
(dotimes (i n s)
(incf s i))))

and you aren't even able to copy code without making typoes.

There is no point in explaining you anything here, it just doesn't
stick. Go read a lisp tutorial. http://cliki.net/Onlin...

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

rpw3

11/8/2015 11:16:00 AM

0

CAI GENGYANG <gengyangcai@gmail.com> wrote:
+----------
| This is a function to return the sum of the numbers less than n.
|
| (defun sum (n)
| (let ((s 0))
| (dotimes (i n s)
| (incf s i))))
+---------- ^----- [I have corrected your typo. --rpw3]

The above is a very silly way to compute the sum of
the integers less than N. The following is much better
[often attributed to Carl Friedrich Gauss circa 1785]:

> (defun sum (n)
(/ (* n (1- n)) 2))

SUM
> (sum 20)

190
> (sum 100)

4950
>


-Rob

p.s. While your phrasing of the problem is consistent
with the code you wrote, the usual statement of the problem
[and the way it was assigned to Gauss] is to compute the sum
of the integers from 1 *through* N, inclusive, for which the
answer would be:

> (defun sum (n)
(/ (* n (1+ n)) 2))

SUM
> (sum 20)

210
> (sum 100)

5050
>

-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <http://rpw...
San Mateo, CA 94403

gengyangcai

11/8/2015 1:29:00 PM

0

Yup ... this is indeed an easier way to compute the sum of integers less than n :

CL-USER 8 : 2 > (defun sum (n)
(/ (* n (1+ n)) 2))
SUM

CL-USER 9 : 2 > (sum 30)
465

CL-USER 10 : 2 > (sum 60)
1830


On Sunday, November 8, 2015 at 7:25:07 PM UTC+8, Rob Warnock wrote:
> CAI GENGYANG <gengyangcai@gmail.com> wrote:
> +----------
> | This is a function to return the sum of the numbers less than n.
> |
> | (defun sum (n)
> | (let ((s 0))
> | (dotimes (i n s)
> | (incf s i))))
> +---------- ^----- [I have corrected your typo. --rpw3]
>
> The above is a very silly way to compute the sum of
> the integers less than N. The following is much better
> [often attributed to Carl Friedrich Gauss circa 1785]:
>
> > (defun sum (n)
> (/ (* n (1- n)) 2))
>
> SUM
> > (sum 20)
>
> 190
> > (sum 100)
>
> 4950
> >
>
>
> -Rob
>
> p.s. While your phrasing of the problem is consistent
> with the code you wrote, the usual statement of the problem
> [and the way it was assigned to Gauss] is to compute the sum
> of the integers from 1 *through* N, inclusive, for which the
> answer would be:
>
> > (defun sum (n)
> (/ (* n (1+ n)) 2))
>
> SUM
> > (sum 20)
>
> 210
> > (sum 100)
>
> 5050
> >
>
> -----
> Rob Warnock <rpw3@rpw3.org>
> 627 26th Avenue <http://rpw...
> San Mateo, CA 94403

Pascal J. Bourguignon

11/8/2015 4:12:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:
> On Sunday, November 8, 2015 at 7:25:07 PM UTC+8, Rob Warnock wrote:
>> CAI GENGYANG <gengyangcai@gmail.com> wrote:
>> +----------
>> | This is a function to return the sum of the numbers less than n.
>> |
>> | (defun sum (n)
>> | (let ((s 0))
>> | (dotimes (i n s)
>> | (incf s i))))
>> +---------- ^----- [I have corrected your typo. --rpw3]
> [â?¦]
> Yup ... this is indeed an easier way to compute the sum of integers less than n :

So what do LET S 0 DOTIMES I N S INCF mean?


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

gengyangcai

11/9/2015 8:57:00 AM

0

This is the equivalent expression in C (from PG's book) :

(defun sum(n) (int sum(int n){
(let ((s 0)) ==== int i, s = 0;
(dotimes (i n s) for(i = 0; i < n; i++)
(incf s i)))) s += i;
return(s);
}

LET is special form for variable binding. Bindings are described in two element lists where the first element specifies name and the second is code to compute its value, or single variable without default initialization. There are also declarations possible before body. ---http://jtra.cz/stuff/lisp/scl...

s = ?? --- can't find a working definition of s

0 --- ?? (Guess it is just a starting reference point (sum of integers after n, everything after 0)

i --- ?? (Guess it is just a letter to indicate the start of the array)

n is simply n as defined in the original question

s --- ?? (can't find a working definition)

dotimes iterates over a series of integers --- http://clhs.lisp.se/Body/m_...

INCF macro modifies a place with numeric value. Its value is incremented by increment number. Default increment is 1. ---- http://jtra.cz/stuff/lisp/sclr...


On Monday, November 9, 2015 at 12:11:48 AM UTC+8, informatimago wrote:
> CAI GENGYANG <gengyangcai@gmail.com> writes:
> > On Sunday, November 8, 2015 at 7:25:07 PM UTC+8, Rob Warnock wrote:
> >> CAI GENGYANG <gengyangcai@gmail.com> wrote:
> >> +----------
> >> | This is a function to return the sum of the numbers less than n.
> >> |
> >> | (defun sum (n)
> >> | (let ((s 0))
> >> | (dotimes (i n s)
> >> | (incf s i))))
> >> +---------- ^----- [I have corrected your typo. --rpw3]
> > [...]
> > Yup ... this is indeed an easier way to compute the sum of integers less than n :
>
> So what do LET S 0 DOTIMES I N S INCF mean?
>
>
> --
> __Pascal Bourguignon__ http://www.informat...
> "The factory of the future will have only two employees, a man and a
> dog. The man will be there to feed the dog. The dog will be there to
> keep the man from touching the equipment." -- Carl Bass CEO Autodesk

smh

11/9/2015 9:48:00 AM

0

The sum of integers less than N is negative infinity.

Pascal J. Bourguignon

11/9/2015 9:54:00 AM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> This is the equivalent expression in C (from PG's book) :
>
> (defun sum(n) (int sum(int n){
> (let ((s 0)) ==== int i, s = 0;
> (dotimes (i n s) for(i = 0; i < n; i++)
> (incf s i)))) s += i;
> return(s);
> }
>

> LET is special form for variable binding. Bindings are described in
> two element lists where the first element specifies name and the
> second is code to compute its value, or single variable without
> default initialization. There are also declarations possible before
> body. ---http://jtra.cz/stuff/lisp/scl...
>
> s = ?? --- can't find a working definition of s
>
> 0 --- ?? (Guess it is just a starting reference point (sum of integers
> after n, everything after 0)
>
> i --- ?? (Guess it is just a letter to indicate the start of the array)
>
> n is simply n as defined in the original question
>
> s --- ?? (can't find a working definition)
>
> dotimes iterates over a series of integers --- http://clhs.lisp.se/Body/m_...
>
> INCF macro modifies a place with numeric value. Its value is
> incremented by increment number. Default increment is 1. ----
> http://jtra.cz/stuff/lisp/sclr...


It's not good enought to search a definition on google and copy-paste it
in your message.

You have to read those definitions and to understand them too.

Read this article:

https://groups.google.c...!original/comp.lang.lisp/wniJmJrA1Dc/uaq1768J4b4J
https://groups.google.c...!msg/comp.lang.lisp/wniJmJrA1Dc/uaq1768J4b4J

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Barry Margolin

11/9/2015 3:49:00 PM

0

In article <09b86461-ab38-4914-a5ca-2e5ba6466d3a@googlegroups.com>,
CAI GENGYANG <gengyangcai@gmail.com> wrote:

> This is the equivalent expression in C (from PG's book) :
>
> (defun sum(n) (int sum(int n){
> (let ((s 0)) ==== int i, s = 0;
> (dotimes (i n s) for(i = 0; i < n; i++)
> (incf s i)))) s += i;
> return(s);
> }
>
> LET is special form for variable binding. Bindings are described in two
> element lists where the first element specifies name and the second is code
> to compute its value, or single variable without default initialization.
> There are also declarations possible before body.
> ---http://jtra.cz/stuff/lisp/scl...
>
> s = ?? --- can't find a working definition of s

Wow, you really don't get it, do you? S is the variable you're binding
with LET.

>
> 0 --- ?? (Guess it is just a starting reference point (sum of integers after
> n, everything after 0)

0 is the initial value you're giving to S.

>
> i --- ?? (Guess it is just a letter to indicate the start of the array)

It's the variable that DOTIMES binds to the step in the iteration.

>
> n is simply n as defined in the original question
>
> s --- ?? (can't find a working definition)

It's the S variable you bound above.

>
> dotimes iterates over a series of integers ---
> http://clhs.lisp.se/Body/m_...

Right, and I will contain the successive elements in the set.

>
> INCF macro modifies a place with numeric value. Its value is incremented by
> increment number. Default increment is 1. ----
> http://jtra.cz/stuff/lisp/sclr...

Right. So this adds I to S.

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

Richard Fateman

11/9/2015 4:35:00 PM

0



I there is a tee shirt that should be worn
while typing responses to Cai. It says..

"I can explain it to you but
I can't understand it for you."

Kaz Kylheku

11/9/2015 5:30:00 PM

0

On 2015-11-09, CAI GENGYANG <gengyangcai@gmail.com> wrote:
> This is the equivalent expression in C (from PG's book) :
>
> (defun sum(n) (int sum(int n){
> (let ((s 0)) ==== int i, s = 0;
> (dotimes (i n s) for(i = 0; i < n; i++)
> (incf s i)))) s += i;
> return(s);
> }
>
> LET is special form for variable binding. Bindings are described in two
> element lists where the first element specifies name and the second is code
> to compute its value, or single variable without default initialization.
> There are also declarations possible before body.
> ---http://jtra.cz/stuff/lisp/scl...
>
> s = ?? --- can't find a working definition of s

I am now convinced this is a troll.

This is not what genuine, home-grown stupidity looks like.