[ create a new paste ] login | about

Link: http://codepad.org/Oh8ddh9e    [ raw code | output | fork ]

Ruby, pasted on Nov 29:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    #!/usr/bin/env ruby
    
    def rprod(k, rv, current, *nums)
      puts "#{rv} * #{current}"
      k.call(0) if current == 0 || rv == 0
      nums.empty? ? (rv * current) : rprod(k, rv * current, *nums)
    end
    
    def prod(first, *rest)
      callcc { |k| rprod(k, first, *rest) }
    end
    
    puts "Seq 1:  #{prod(1, 2, 3, 4, 5, 6)}"
    puts ""
    puts "Seq 2:  #{prod(1, 2, 0, 3, 4, 5, 6)}"


Output:
1
2
3
4
5
6
7
8
9
10
1 * 2
2 * 3
6 * 4
24 * 5
120 * 6
Seq 1:  720

1 * 2
2 * 0
Seq 2:  0


Create a new paste based on this one


Comments: