Commits on Oct 28, 2011

  1. Implement natural numbers

    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    29e4596 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    3bf39f9 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    6abee8b View commit details
    Browse the repository at this point in the history
  4. Implement booleans

    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    b28aee1 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    819deaa View commit details
    Browse the repository at this point in the history
  6. Implement NOT, AND and OR for booleans

    def not(b)
      if b
        false
      else
        true
      end
    end
    
    def and(a, b)
      if a
        if b
          true
        else
          false
        end
      else
        false
      end
    end
    
    def or(a, b)
      if a
        true
      else
        if b
          true
        else
          false
        end
      end
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    526f24d View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    98fb5b5 View commit details
    Browse the repository at this point in the history
  8. Implement IS_LESS_OR_EQUAL and IS_EQUAL for natural numbers

    def less_or_equal?(m, n)
      (m - n).zero? # 0 is the smallest number
    end
    
    def equal?(m, n)
      m <= n && n <= m
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    77f4c5f View commit details
    Browse the repository at this point in the history
  9. Implement broken, cheating FACTORIAL for natural numbers

    def factorial(n)
      if n.zero?
        1
      else
        n * factorial(n - 1)
      end
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    32302b5 View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    f94cec0 View commit details
    Browse the repository at this point in the history
  11. Configuration menu
    Copy the full SHA
    657df1e View commit details
    Browse the repository at this point in the history
  12. Define Y and Z combinators

    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    c0c37ac View commit details
    Browse the repository at this point in the history
  13. Configuration menu
    Copy the full SHA
    e0b5836 View commit details
    Browse the repository at this point in the history
  14. Implement DIV for natural numbers

    def div(m, n)
      if n <= m
        div(m - n, n) + 1
      else
        0
      end
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    17e5858 View commit details
    Browse the repository at this point in the history
  15. Implement MOD for natural numbers

    def mod(m, n)
      if n <= m
        mod(m - n, n)
      else
        m
      end
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    1b6b8df View commit details
    Browse the repository at this point in the history
  16. Implement pairs

    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    e80e1a1 View commit details
    Browse the repository at this point in the history
  17. Configuration menu
    Copy the full SHA
    89d3c0b View commit details
    Browse the repository at this point in the history
  18. Configuration menu
    Copy the full SHA
    b65af2d View commit details
    Browse the repository at this point in the history
  19. Implement FIRST for lists

    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    4bca913 View commit details
    Browse the repository at this point in the history
  20. Implement REST for lists

    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    39e2846 View commit details
    Browse the repository at this point in the history
  21. Implement RANGE for lists

    def range(m, n)
      if m <= n
        range(m + 1, n).unshift(m)
      else
        []
      end
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    5a57d32 View commit details
    Browse the repository at this point in the history
  22. Implement SUM for lists

    def sum(l)
      if l.empty?
        0
      else
        l.first + sum(l[1..-1])
      end
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    669c484 View commit details
    Browse the repository at this point in the history
  23. Implement PRODUCT for lists

    def product(l)
      if l.empty?
        1
      else
        l.first * product(l[1..-1])
      end
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    0908d77 View commit details
    Browse the repository at this point in the history
  24. Extract INJECT from SUM and PRODUCT

    def inject(l, x, &block)
      if l.empty?
        x
      else
        inject(l[1..-1], block.call(x, l.first), &block)
      end
    end
    tomstuart committed Oct 28, 2011
    2 Configuration menu
    Copy the full SHA
    5e10048 View commit details
    Browse the repository at this point in the history
  25. Implement CONCAT for lists

    def concat(k, l)
      if k.empty?
        l
      else
        concat(k[1..-1], l).unshift(k.first)
      end
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    8c412ac View commit details
    Browse the repository at this point in the history
  26. Implement PUSH for lists

    def push(l, x)
      l.concat([].unshift(x))
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    1d19a83 View commit details
    Browse the repository at this point in the history
  27. Implement REVERSE for lists

    def reverse(l)
      if l.empty?
        []
      else
        reverse(l[1..-1]).push(l.first)
      end
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    f3812da View commit details
    Browse the repository at this point in the history
  28. Extract FOLD from CONCAT and REVERSE

    def fold(l, x, &block)
      if l.empty?
        x
      else
        block.call(fold(l[1..-1], x, &block), l.first)
      end
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    aadb9c5 View commit details
    Browse the repository at this point in the history
  29. Implement INCREMENT_ALL for lists

    def increment_all(k)
      fold(k, []) { |l, n| l.unshift(n + 1) }
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    27ffc85 View commit details
    Browse the repository at this point in the history
  30. Implement DOUBLE_ALL for lists

    def double_all(k)
      fold(k, []) { |l, n| l.unshift(n * 2) }
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    b7937e9 View commit details
    Browse the repository at this point in the history
  31. Extract MAP from INCREMENT_ALL and DOUBLE_ALL

    def map(k, &block)
      fold(k, []) { |l, x| l.unshift(block.call(x)) }
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    b31dde6 View commit details
    Browse the repository at this point in the history
  32. Implement TO_DIGITS for natural numbers

    def to_digits(n)
      (n <= 9 ? [] : to_digits(n / 10)).push(n % 10)
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    97ed146 View commit details
    Browse the repository at this point in the history
  33. Configuration menu
    Copy the full SHA
    096db61 View commit details
    Browse the repository at this point in the history
  34. Implement FizzBuzz

    def fizzbuzz(m)
      (1..m).map { |n|
        if (n % 15).zero?
          'FizzBuzz'
        elsif (n % 3).zero?
          'Fizz'
        elsif (n % 5).zero?
          'Buzz'
        else
          n.to_s
        end
      }
    end
    tomstuart committed Oct 28, 2011
    Configuration menu
    Copy the full SHA
    4f2ab74 View commit details
    Browse the repository at this point in the history

Commits on Mar 29, 2012

  1. Fix specs to work with Ruby 1.9.3+

    The ability to refer to constants in an included module was actually a bug
    in Ruby 1.9.2, so the specs were failing on later versions where this bug has
    been fixed.
    
    The solution is to define the example blocks inside the module itself, since
    that's the only place where the unqualified constants should be visible.
    
    rspec/rspec-core#506
    http://bugs.ruby-lang.org/issues/5657
    https://twitter.com/dchelimsky/status/185335729738616833
    tomstuart committed Mar 29, 2012
    Configuration menu
    Copy the full SHA
    cb7addd View commit details
    Browse the repository at this point in the history