Jump to content

Y combinator: Difference between revisions

Dialects of BASIC moved to the BASIC section.
(Dialects of BASIC moved to the BASIC section.)
Line 823:
(* ****** ****** *)
</syntaxhighlight>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
FreeBASIC does not support nested functions, lambda expressions or functions inside nested types
<syntaxhighlight lang="freebasic">Function Y(f As String) As String
Y = f
End Function
 
Function fib(n As Long) As Long
Dim As Long n1 = 0, n2 = 1, k, sum
For k = 1 To Abs(n)
sum = n1 + n2
n1 = n2
n2 = sum
Next k
Return Iif(n < 0, (n1 * ((-1) ^ ((-n)+1))), n1)
End Function
 
Function fac(n As Long) As Long
Dim As Long r = 1, i
For i = 2 To n
r *= i
Next i
Return r
End Function
 
Function execute(s As String, n As Integer) As Long
Return Iif (s = "fac", fac(n), fib(n))
End Function
 
Sub test(nombre As String)
Dim f As String: f = Y(nombre)
Print !"\n"; f; ":";
For i As Integer = 1 To 10
Print execute(f, i);
Next i
End Sub
 
test("fac")
test("fib")
Sleep</syntaxhighlight>
{{out}}
<pre>fac: 1 2 6 24 120 720 5040 40320 362880 3628800
fib: 1 1 2 3 5 8 13 21 34 55</pre>
 
==={{header|VBA}}===
{{trans|Phix}}
The IIf as translation of Iff can not be used as IIf executes both true and false parts and will cause a stack overflow.
<syntaxhighlight lang="vb">Private Function call_fn(f As String, n As Long) As Long
call_fn = Application.Run(f, f, n)
End Function
Private Function Y(f As String) As String
Y = f
End Function
Private Function fac(self As String, n As Long) As Long
If n > 1 Then
fac = n * call_fn(self, n - 1)
Else
fac = 1
End If
End Function
Private Function fib(self As String, n As Long) As Long
If n > 1 Then
fib = call_fn(self, n - 1) + call_fn(self, n - 2)
Else
fib = n
End If
End Function
Private Sub test(name As String)
Dim f As String: f = Y(name)
Dim i As Long
Debug.Print name
For i = 1 To 10
Debug.Print call_fn(f, i);
Next i
Debug.Print
End Sub
 
Public Sub main()
test "fac"
test "fib"
End Sub</syntaxhighlight>{{out}}
<pre>fac
1 2 6 24 120 720 5040 40320 362880 3628800
fib
1 1 2 3 5 8 13 21 34 55 </pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">sub fac(self$, n)
if n > 1 then
return n * execute(self$, self$, n - 1)
else
return 1
end if
end sub
sub fib(self$, n)
if n > 1 then
return execute(self$, self$, n - 1) + execute(self$, self$, n - 2)
else
return n
end if
end sub
sub test(name$)
local i
print name$, ": ";
for i = 1 to 10
print execute(name$, name$, i);
next
print
end sub
 
test("fac")
test("fib")</syntaxhighlight>
 
=={{header|BlitzMax}}==
Line 2,867 ⟶ 2,987:
y execute . 55 ok
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
FreeBASIC does not support nested functions, lambda expressions or functions inside nested types
<syntaxhighlight lang="freebasic">Function Y(f As String) As String
Y = f
End Function
 
Function fib(n As Long) As Long
Dim As Long n1 = 0, n2 = 1, k, sum
For k = 1 To Abs(n)
sum = n1 + n2
n1 = n2
n2 = sum
Next k
Return Iif(n < 0, (n1 * ((-1) ^ ((-n)+1))), n1)
End Function
 
Function fac(n As Long) As Long
Dim As Long r = 1, i
For i = 2 To n
r *= i
Next i
Return r
End Function
 
Function execute(s As String, n As Integer) As Long
Return Iif (s = "fac", fac(n), fib(n))
End Function
 
Sub test(nombre As String)
Dim f As String: f = Y(nombre)
Print !"\n"; f; ":";
For i As Integer = 1 To 10
Print execute(f, i);
Next i
End Sub
 
test("fac")
test("fib")
Sleep</syntaxhighlight>
{{out}}
<pre>fac: 1 2 6 24 120 720 5040 40320 362880 3628800
fib: 1 1 2 3 5 8 13 21 34 55</pre>
 
 
=={{header|GAP}}==
Line 5,379 ⟶ 5,455:
my_fix "h" = "h" my_fix "h"</syntaxhighlight>
Note that this equation is solved using the next fixed point combinator in the hierarchy.
 
=={{header|VBA}}==
{{trans|Phix}}
The IIf as translation of Iff can not be used as IIf executes both true and false parts and will cause a stack overflow.
<syntaxhighlight lang="vb">Private Function call_fn(f As String, n As Long) As Long
call_fn = Application.Run(f, f, n)
End Function
Private Function Y(f As String) As String
Y = f
End Function
Private Function fac(self As String, n As Long) As Long
If n > 1 Then
fac = n * call_fn(self, n - 1)
Else
fac = 1
End If
End Function
Private Function fib(self As String, n As Long) As Long
If n > 1 Then
fib = call_fn(self, n - 1) + call_fn(self, n - 2)
Else
fib = n
End If
End Function
Private Sub test(name As String)
Dim f As String: f = Y(name)
Dim i As Long
Debug.Print name
For i = 1 To 10
Debug.Print call_fn(f, i);
Next i
Debug.Print
End Sub
 
Public Sub main()
test "fac"
test "fib"
End Sub</syntaxhighlight>{{out}}
<pre>fac
1 2 6 24 120 720 5040 40320 362880 3628800
fib
1 1 2 3 5 8 13 21 34 55 </pre>
 
=={{header|Verbexx}}==
Line 5,564 ⟶ 5,594:
{{out}}
<syntaxhighlight lang="xquery">720 8</syntaxhighlight>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">sub fac(self$, n)
if n > 1 then
return n * execute(self$, self$, n - 1)
else
return 1
end if
end sub
sub fib(self$, n)
if n > 1 then
return execute(self$, self$, n - 1) + execute(self$, self$, n - 2)
else
return n
end if
end sub
sub test(name$)
local i
print name$, ": ";
for i = 1 to 10
print execute(name$, name$, i);
next
print
end sub
 
test("fac")
test("fib")</syntaxhighlight>
 
=={{header|zkl}}==
512

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.