Y combinator: Difference between revisions

Content added Content deleted
(Undo revision 345797 by Ismael-vc (talk) Reverted change that had deleted a lot of the page)
m (→‎{{header|Julia}}: Restored the Julia version of Ismaei-vc)
Line 3,442: Line 3,442:
=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>
<lang julia>
_
julia> """
_ _ _(_)_ | Documentation: https://docs.julialang.org
# Y combinator
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.6.3 (2021-09-23)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |


julia> using Markdown
* `λf. (λx. f (x x)) (λx. f (x x))`

"""
julia> @doc md"""
# Y Combinator

$λf. (λx. f (x x)) (λx. f (x x))$
""" ->
Y = f -> (x -> x(x))(y -> f((t...) -> y(y)(t...)))
Y = f -> (x -> x(x))(y -> f((t...) -> y(y)(t...)))
Y
</lang>
</lang>


Line 3,453: Line 3,465:


<lang julia>
<lang julia>
julia> fac = f -> (n -> n < 2 ? 1 : n * f(n - 1))
julia> "# Factorial"
#9 (generic function with 1 method)
fac = f -> (n -> n < 2 ? 1 : n * f(n - 1))


julia> fib = f -> (n -> n == 0 ? 0 : (n == 1 ? 1 : f(n - 1) + f(n - 2)))
julia> "# Fibonacci"
#13 (generic function with 1 method)
fib = f -> (n -> n == 0 ? 0 : (n == 1 ? 1 : f(n - 1) + f(n - 2)))


julia> [Y(fac)(i) for i = 1:10]
julia> Y(fac).(1:10)
10-element Array{Any,1}:
10-element Vector{Int64}:
1
1
2
2
Line 3,472: Line 3,484:
3628800
3628800


julia> [Y(fib)(i) for i = 1:10]
julia> Y(fib).(1:10)
10-element Array{Any,1}:
10-element Vector{Int64}:
1
1
1
1