Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #53253

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

From "Robert L." <No_spamming@noWhere_7073.org>
Newsgroups comp.lang.forth
Subject A function to return the sum of the numbers less than n
Date 2016-12-10 10:47 +0000
Organization Aioe.org NNTP Server
Message-ID <o2gmg2$1gk5$1@gioia.aioe.org> (permalink)

Show all headers | View raw


> > 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))))

Ruby:

def sum n
  (0...n).reduce(0, :+)
end

sum 0
    ==>0
sum 1
    ==>0
sum 22_000
    ==>241989000

In Forth?

-- 
"If a government uses the instruments of power in its hands for the purpose of
leading a people to ruin, then rebellion is not only the right but also the
duty of every individual citizen."

Back to comp.lang.forth | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

A function to return the sum of the numbers less than n "Robert L." <No_spamming@noWhere_7073.org> - 2016-12-10 10:47 +0000
  Re: A function to return the sum of the numbers less than n Julian Fondren <julian.fondren@gmail.com> - 2016-12-10 03:19 -0800
    Re: A function to return the sum of the numbers less than n foxaudioresearch@gmail.com - 2016-12-10 03:58 -0800
    Re: A function to return the sum of the numbers less than n anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-10 12:48 +0000
      Re: A function to return the sum of the numbers less than n Julian Fondren <julian.fondren@gmail.com> - 2016-12-10 05:29 -0800
        Re: A function to return the sum of the numbers less than n pahihu <pahihu@gmail.com> - 2016-12-10 06:37 -0800
        Re: A function to return the sum of the numbers less than n anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-10 17:08 +0000
  Re: A function to return the sum of the numbers less than n jean-pierre@buechi-schmitt.ch - 2016-12-10 07:04 -0800
    Re: A function to return the sum of the numbers less than n awegel@arcor.de (Alexander Wegel) - 2016-12-10 22:59 +0100

csiph-web