Recursive Proc in Crystal

Viewed 606

Is recursive proc posible in Crystal?

Something like lambda in Ruby

I'm trying to do a y-combinator in Crystal,something like Ruby one:

puts -> {
  fact_improver = ->(partial) {
    -> (n) { n.zero? ? 1 : n * partial.(n-1) }
  }
  y = ->(f) {
    ->(x) { f.(->(v) { x.(x).(v) }) }.(
    ->(x) { f.(->(v) { x.(x).(v) }) }
    )
  }
  fact = y.(fact_improver)
  fact = fact_improver.(fact)
  fact.(100)
}.()

The above code was taken from Y Not- Adventures in Functional Programming

1 Answers
Related