# A hypothetical example of creating a data structure out of thin air (that is,
# not using any existing data structures).
#
# With "data structure", I mean something that contains multiple values and is
# representable by a variable.
#
# Other data structures, such as array and hash, could be implemented with
# pairs.
#
# This method was invented by Alonzo Church before computers existed.

#   pair = cons "foo", 42
#
#   left  pair        # ⇒ "foo"
#   right pair        # ⇒ 42

# Construct a pair.
def cons a, b
  lambda {|picker| picker.call a, b }
end

# Pick the first item.
def left pair
  pair.call lambda {|a, b| a }
end

# Pick the second item.
def right pair
  pair.call lambda {|a, b| b }
end