Skip to content

Instantly share code, notes, and snippets.

@kevinrutherford
Last active December 27, 2015 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinrutherford/7277260 to your computer and use it in GitHub Desktop.
Save kevinrutherford/7277260 to your computer and use it in GitHub Desktop.
Pair = Struct.new(:head, :tail)
Either = Struct.new(:val)
class Left < Either
def value(pair, hylo)
pair.head
end
end
class Right < Either
def value(pair, hylo)
# Forgive me, Spaghetti Monster, for I have sinned.
pair.tail.call(val.head, hylo.call(val.tail))
end
end
list_hylo = -> destruct_f,pair {
hylo = -> initial { destruct_f.call(initial).value(pair, hylo) }
}
# mult :: Int -> Int -> Int
mult = -> a,b { a * b }
# destruct_count :: Int -> Either () (Int,Int)
# LOL
destruct_count = -> n {
if n == 0
Left.new( nil )
else
Right.new( Pair.new(n, n-1) )
end
}
fac = list_hylo.call(destruct_count, Pair.new(1, mult))
(0..10).each do |n|
puts "factorial #{n} = #{fac.call(n)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment