[LUAU] doing 'e'

Vince Hoang vince at litrium.com
Thu Dec 1 01:01:32 PST 2005


On Wed, Nov 30, 2005 at 06:56:48AM -1000, Jim Thompson wrote:
> Lets see a perl/python/... equivalent to this lisp exercise for 
> obtaining 'e':

I will pass on perl, but here is my attempt in ruby. I would have
made it more functional/lisp-like, but I have not fully grokked
using yield.

  #! /usr/bin/env ruby
  
  class Array
    def avg
      inject { |sum, n| sum + n } / length.to_f
    end
  end
  
  def wait_time
    sum, times = 0, 0
    while sum <= 1.0
      sum += rand
      times += 1
    end
    times
  end
  
  def wait_times(trials)
    list = []
    trials.times do |t|
      list.push wait_time
    end
    list
  end
  
  def avg_wait_time(trials)
    wait_times(trials).avg
  end
  
  puts 'Some wait times: ' + wait_times(10).join(' ')
  
  trials = [ 1, 10, 100, 1000, 10000, 100000 ]
  print 'Average wait times:'
  trials.each do |t|
    print ' ' + avg_wait_time(t).to_s
  end

Visit http://tryruby.hobix.com/ for an amazingly interactive
tutorial to ruby.

-Vince



More information about the LUAU mailing list