Last active
December 27, 2015 05:49
-
-
Save kevinrutherford/7277260 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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