Comments

You must log in or register to comment.

5

musou wrote (edited )

it doesn't let you write a recursive function the normal way, instead you write a function that takes a function as input (so you can introudce a stand-in name for the recursive call) and returns the function you want to write recursively except you call the input function in place of the recursive step. then you pass that through the y combinator to get it to work. here's an untested example in elixir because it's what i think in

non_recursive_factorial = fn f ->
  fn
    0 -> 1
    n -> n * f.(n - 1) 
  end
end

# this will just return a useless function, not the answer. :[
non_recursive_factorial.(5)

# gotta run it through the y combinator.
recursive_factorial = y_combinator.(non_recursive_factorial)

# this should return 120
recursive_factorial.(5)
4

musou wrote

and if you actually want to try it here's a spagehti i found on rosetta code for the y combinator

y_combinator = fn f ->
  (fn x ->
     x.(x)
   end).(fn y ->
    f.(fn z ->
      y.(y).(z)
    end)
  end)
end

i went back and actually did try it and it works using this definition

4

devtesla wrote

y combinator is an incubator for demons that will kill us all

4

musou wrote

i hate that they took the name of a cool math thing. math is for cool people not for bloodthirsty capitalist nerds

3

Moonside wrote

I just checked the Wiki (I have never studied lambda calculus), can you beta reduce Y g = ...stuff... by hand?