From: Ryan Leavengood Date: 2005-10-20T03:59:50+09:00 Subject: Re: Functional with Ruby On 10/19/05, Andreas Semt wrote: > Hello list, > > here a really simple Haskell function, which sums all elements of a list > (from the book "The Haskell School of Expression"): > -------------------------------- > listSum [] = 0 > listSum (x : xs) = x + listSum xs > -------------------------------- > > Which is the best way to say it in Ruby (functional style)? Don't use > 'enum.inject', please! What's wrong with inject? I find this very readable and not any more verbose than the Haskell: irb(main):001:0> a=[1,2,3,4,5,6,7] => [1, 2, 3, 4, 5, 6, 7] irb(main):002:0> a.inject(0){|i,sum| i+sum} => 28 irb(main):003:0> 1+2+3+4+5+6+7 => 28 irb(main):004:0> module Enumerable irb(main):005:1> def list_sum irb(main):006:2> self.inject(0){|i,sum| i+sum} irb(main):007:2> end irb(main):008:1> end => nil irb(main):009:0> a.list_sum => 28 Ryan