Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
19
Task/Catamorphism/EchoLisp/catamorphism.echolisp
Normal file
19
Task/Catamorphism/EchoLisp/catamorphism.echolisp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
;; rem : the foldX family always need an initial value
|
||||
;; fold left a list
|
||||
(foldl + 0 (iota 10)) ;; 0 + 1 + .. + 9
|
||||
→ 45
|
||||
|
||||
;; fold left a sequence
|
||||
(lib 'sequences)
|
||||
(foldl * 1 [ 1 .. 10])
|
||||
→ 362880 ;; 10!
|
||||
|
||||
;; folding left and right
|
||||
(foldl / 1 ' ( 1 2 3 4))
|
||||
→ 8/3
|
||||
(foldr / 1 '(1 2 3 4))
|
||||
→ 3/8
|
||||
|
||||
;;scanl gives the list (or sequence) of intermediate values :
|
||||
(scanl * 1 '( 1 2 3 4 5))
|
||||
→ (1 1 2 6 24 120)
|
||||
44
Task/Catamorphism/FreeBASIC/catamorphism.freebasic
Normal file
44
Task/Catamorphism/FreeBASIC/catamorphism.freebasic
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Type IntFunc As Function(As Integer, As Integer) As Integer
|
||||
|
||||
Function reduce(a() As Integer, f As IntFunc) As Integer
|
||||
'' if array is empty or function pointer is null, return 0 say
|
||||
If UBound(a) = -1 OrElse f = 0 Then Return 0
|
||||
Dim result As Integer = a(LBound(a))
|
||||
For i As Integer = LBound(a) + 1 To UBound(a)
|
||||
result = f(result, a(i))
|
||||
Next
|
||||
Return result
|
||||
End Function
|
||||
|
||||
Function add(x As Integer, y As Integer) As Integer
|
||||
Return x + y
|
||||
End Function
|
||||
|
||||
Function subtract(x As Integer, y As Integer) As Integer
|
||||
Return x - y
|
||||
End Function
|
||||
|
||||
Function multiply(x As Integer, y As Integer) As Integer
|
||||
Return x * y
|
||||
End Function
|
||||
|
||||
Function max(x As Integer, y As Integer) As Integer
|
||||
Return IIf(x > y, x, y)
|
||||
End Function
|
||||
|
||||
Function min(x As Integer, y As Integer) As Integer
|
||||
Return IIf(x < y, x, y)
|
||||
End Function
|
||||
|
||||
Dim a(4) As Integer = {1, 2, 3, 4, 5}
|
||||
Print "Sum is :"; reduce(a(), @add)
|
||||
Print "Difference is :"; reduce(a(), @subtract)
|
||||
Print "Product is :"; reduce(a(), @multiply)
|
||||
Print "Maximum is :"; reduce(a(), @max)
|
||||
Print "Minimum is :"; reduce(a(), @min)
|
||||
Print "No op is :"; reduce(a(), 0)
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
19
Task/Catamorphism/Nim/catamorphism.nim
Normal file
19
Task/Catamorphism/Nim/catamorphism.nim
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import sequtils
|
||||
|
||||
block:
|
||||
let
|
||||
numbers = @[5, 9, 11]
|
||||
addition = foldl(numbers, a + b)
|
||||
substraction = foldl(numbers, a - b)
|
||||
multiplication = foldl(numbers, a * b)
|
||||
words = @["nim", "rod", "is", "cool"]
|
||||
concatenation = foldl(words, a & b)
|
||||
|
||||
block:
|
||||
let
|
||||
numbers = @[5, 9, 11]
|
||||
addition = foldr(numbers, a + b)
|
||||
substraction = foldr(numbers, a - b)
|
||||
multiplication = foldr(numbers, a * b)
|
||||
words = @["nim", "rod", "is", "cool"]
|
||||
concatenation = foldr(words, a & b)
|
||||
2
Task/Catamorphism/Oforth/catamorphism.oforth
Normal file
2
Task/Catamorphism/Oforth/catamorphism.oforth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[ 1, 2, 3, 4, 5 ] reduce(#max)
|
||||
[ "abc", "def", "gfi" ] reduce(#+)
|
||||
34
Task/Catamorphism/Ring/catamorphism.ring
Normal file
34
Task/Catamorphism/Ring/catamorphism.ring
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
n = list(10)
|
||||
for i = 1 to 10
|
||||
n[i] = i
|
||||
next
|
||||
|
||||
see " +: " + cat(10,"+") + nl+
|
||||
" -: " + cat(10,"-") + nl +
|
||||
" *: " + cat(10,"*") + nl +
|
||||
" /: " + cat(10,"/") + nl+
|
||||
" ^: " + cat(10,"^") + nl +
|
||||
"min: " + cat(10,"min") + nl+
|
||||
"max: " + cat(10,"max") + nl+
|
||||
"avg: " + cat(10,"avg") + nl +
|
||||
"cat: " + cat(10,"cat") + nl
|
||||
|
||||
func cat count,op
|
||||
cat = n[1]
|
||||
cat2 = ""
|
||||
for i = 2 to count
|
||||
switch op
|
||||
on "+" cat += n[i]
|
||||
on "-" cat -= n[i]
|
||||
on "*" cat *= n[i]
|
||||
on "/" cat /= n[i]
|
||||
on "^" cat ^= n[i]
|
||||
on "max" cat = max(cat,n[i])
|
||||
on "min" cat = min(cat,n[i])
|
||||
on "avg" cat += n[i]
|
||||
on "cat" cat2 += string(n[i])
|
||||
off
|
||||
next
|
||||
if op = "avg" cat = cat / count ok
|
||||
if op = "cat" decimals(0) cat = string(n[1])+cat2 ok
|
||||
return cat
|
||||
2
Task/Catamorphism/Sidef/catamorphism.sidef
Normal file
2
Task/Catamorphism/Sidef/catamorphism.sidef
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
say (1..10 -> reduce('+'));
|
||||
say (1..10 -> reduce{|a,b| a + b});
|
||||
1
Task/Catamorphism/Wortel/catamorphism-1.wortel
Normal file
1
Task/Catamorphism/Wortel/catamorphism-1.wortel
Normal file
|
|
@ -0,0 +1 @@
|
|||
!/ ^+ [1 2 3] ; returns 6
|
||||
1
Task/Catamorphism/Wortel/catamorphism-2.wortel
Normal file
1
Task/Catamorphism/Wortel/catamorphism-2.wortel
Normal file
|
|
@ -0,0 +1 @@
|
|||
@fold ^+ 1 [1 2 3] ; returns 7
|
||||
Loading…
Add table
Add a link
Reference in a new issue