Why not Python? (No, no, I am not a spy)

dblack@wobblini.net wrote:

Oh, let's :slight_smile: I really wish :: as a synonym for the dot would
disappear. I've never understood what purpose is served by it.

if "::" is used exclusively for class constants, i guess it's slightly
useful, otherwise i tend to agree

Jeff Wood wrote:

heh, darn, bug there ...

# yes assumptions about positive integer numbers...
# factorial of 0 is undefined ...

# complete recursive:

def factorial( x )
return 1 if x < 1

return 1 if x <= 1

x * factorial( x - 1 )
end

# tail-recursive:

def factorial( x, sum = 1 )
return 1 if x < 1

return sum if x <= 1

factorial( ( x - 1 ), ( sum * x ) )
end

# iterative:

def factorial( x )
sum = 1
for i in (1..x).to_a

for i in 1..x

   sum *= i
end
end

And why do you call your products "sum"?

···

--
Florian Frank

uh, yeah, that's what I meant ... darn'd quickie code always bites me in the
butt ...

j.

···

On 12/23/05, Jacob Fugal <lukfugl@gmail.com> wrote:

On 12/23/05, Jeff Wood <jeff.darklight@gmail.com> wrote:
> # complete recursive:
>
> def factorial( x )
> return 0 if x < 1
> x * factorial( x - 1 )
> end
>
> # tail-recursive:
>
> def factorial( x, sum = 1 )
> return 0 if x < 1
> factorial( ( x - 1 ), ( sum * x ) )
> end
>
> # iterative:
>
> def factorial( x )
> sum = 1
> for i in (1..x).to_a
> sum *= i
> end
> end

With bugfixes:

  # complete recursive:
  def factorial( x )
    return 1 if x < 1
    x * factorial( x - 1 )
  end

  # tail-recursive:
  def factorial( x, sum = 1 )
    return sum if x < 1
    factorial( ( x - 1 ), ( sum * x ) )
  end

  # iterative:
  def factorial( x )
    sum = 1
    for i in (1..x).to_a
      sum *= i
    end
    sum
  end

:slight_smile:

Jacob Fugal

--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood

Hi --

···

On Fri, 23 Dec 2005, Gene Tani wrote:

dblack@wobblini.net wrote:

Oh, let's :slight_smile: I really wish :: as a synonym for the dot would
disappear. I've never understood what purpose is served by it.

if "::" is used exclusively for class constants, i guess it's slightly
useful, otherwise i tend to agree

For constant lookup it seems perfectly fine. I just don't know what
it's doing also being a dot synonym.

David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!