Domanda

ho cercato di definire un generatore di punto fisso in C # che si vede in molti linguaggi funzionali. Credo foldr è solitamente definita in termini di un generatore di punto fisso volte. Io mostrare che è la definizione Haskell e poi quello che ho in C #. Ogni aiuto è molto apprezzato.

//Haskell
fix f = f (fix f)

//C# (Many attempts)
public static Func<Func<T, T>, T> Combinator1<T>(this Func<T, T> f)
{
    return x => f(Combinator1(f)(x));
}
public static Func<Func<T, T>, T> Combinator2<T>(this Func<T, T> f)
{
    return x => x(Combinator2(x)(f));
}
public static Func<T, U> Combinator3<T, U>(Func<Func<T, U>, Func<T, U>> f)
{
    return f(x => Combinator3(f)(x));
}
È stato utile?

Soluzione

I don't have much understanding of haskell or this operator. But I have read an article from Mads Torgersen about implementing the Y/Fix combinator using C# lambda expressions. It may be of some use to you, here is the link.

And here is the final method he implements:

public Func<T, T> Fix<T>(Func<Func<T,T>, Func<T,T>> F) {
  return t => F(Fix(F))(t);
}

Altri suggerimenti

First of all, the Y combinator is a particular implementation in untyped lambda calculus. We are talking more generally about fixed point combinators.

All the answers given here show excellently why fixed point combinators don't really make sense without currying. The one given by Lukazoid is not as general as it should be. It has this type (in Haskell notation):

lukazoidFix :: ((a -> b) -> a -> b) -> a -> b

A real fixed point combinator should be much more polymorphic.

fix :: (a -> a) -> a
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top