Catamorphism: Difference between revisions
Content added Content deleted
(Double entry of uBasic/4tH - moved to proper entry) |
(Added another uBasic/4tH version) |
||
Line 2,783: | Line 2,783: | ||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
uBasic/4tH has only got one single array so passing its address makes little sense. Instead, its bounds are passed. |
uBasic/4tH has only got one single array so passing its address makes little sense. Instead, its bounds are passed. |
||
<syntaxhighlight lang="uBasic/4tH">For x = 1 To 5 : @(x-1) = x : Next ' initialize array |
|||
' try different reductions |
|||
Print "Sum is : "; FUNC(_Reduce(_add, 5)) |
|||
Print "Difference is : "; FUNC(_Reduce(_subtract, 5)) |
|||
Print "Product is : "; FUNC(_Reduce(_multiply, 5)) |
|||
Print "Maximum is : "; FUNC(_Reduce(_max, 5)) |
|||
Print "Minimum is : "; FUNC(_Reduce(_min, 5)) |
|||
End |
|||
' several functions |
|||
_add Param (2) : Return (a@ + b@) |
|||
_subtract Param (2) : Return (a@ - b@) |
|||
_multiply Param (2) : Return (a@ * b@) |
|||
_min Param (2) : Return (Min (a@, b@)) |
|||
_max Param (2) : Return (Max (a@, b@)) |
|||
_Reduce |
|||
Param (2) ' function and array size |
|||
Local (2) ' loop index and result |
|||
' set result and iterate array |
|||
d@ = @(0) : For c@ = 1 To b@-1 : d@ = FUNC(a@ (d@, @(c@))) : Next |
|||
Return (d@)</syntaxhighlight> |
|||
This version incorporates a "no op" as well. |
|||
<syntaxhighlight lang="text">Push 5, 4, 3, 2, 1: s = Used() - 1 |
<syntaxhighlight lang="text">Push 5, 4, 3, 2, 1: s = Used() - 1 |
||
For x = 0 To s: @(x) = Pop(): Next |
For x = 0 To s: @(x) = Pop(): Next |
||
Line 2,820: | Line 2,843: | ||
0 OK, 0:378 |
0 OK, 0:378 |
||
</pre> |
</pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
<syntaxhighlight lang="vb">Public Sub reduce() |
<syntaxhighlight lang="vb">Public Sub reduce() |