¿Qué hace este combinador?s (sk)
-
12-12-2019 - |
Pregunta
Ahora entiendo el tipo de firma de s (s k)
:
s (s k) :: ((t1 -> t2) -> t1) -> (t1 -> t2) -> t1
Y puedo crear ejemplos que funcionan sin errores en la herramienta Haskell WinGHCi:
Ejemplo:
s (s k) (\g -> 2) (\x -> 3)
devoluciones 2
.
Ejemplo:
s (s k) (\g -> g 3) successor
devoluciones 4
.
dónde successor
se define así:
successor = (\x -> x + 1)
Aún así todavía no tengo una sensación intuitiva para qué s (s k)
hace.
El combinador s (s k)
toma dos funciones cualesquiera f
y g
.Que hace s (s k)
hazlo f
y g
?Me darías el panorama en que s (s k)
¿Por favor?
Solución
Muy bien, veamos qué S (S K)
medio.Voy a utilizar estas definiciones:
S = \x y z -> x z (y z)
K = \x y -> x
S (S K) = (\x y z -> x z (y z)) ((\x y z -> x z (y z)) (\a b -> a)) -- rename bound variables in K
= (\x y z -> x z (y z)) (\y z -> (\a b -> a) z (y z)) -- apply S to K
= (\x y z -> x z (y z)) (\y z -> (\b -> z) (y z)) -- apply K to z
= (\x y z -> x z (y z)) (\y z -> z) -- apply (\_ -> z) to (y z)
= (\x y z -> x z (y z)) (\a b -> b) -- rename bound variables
= (\y z -> (\a b -> b) z (y z)) -- apply S to (\a b -> b)
= (\y z -> (\b -> b) (y z)) -- apply (\a b -> b) to z
= (\y z -> y z) -- apply id to (y z)
Como puedes ver, es solo ($)
con un tipo más específico.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow