Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,14 @@
Integer f(Integer n)
=> if (n > 0)
then n - m(f(n-1))
else 1;
Integer m(Integer n)
=> if (n > 0)
then n - f(m(n-1))
else 0;
shared void run() {
printAll((0:20).map(f));
printAll((0:20).map(m));
}

View file

@ -0,0 +1,33 @@
' FB 1.05.0 Win64
' Need forward declaration of M as it's used
' by F before its defined
Declare Function M(n As Integer) As Integer
Function F(n As Integer) As Integer
If n = 0 Then
Return 1
End If
Return n - M(F(n - 1))
End Function
Function M(n As Integer) As Integer
If n = 0 Then
Return 0
End If
Return n - F(M(n - 1))
End Function
Dim As Integer n = 24
Print "n :";
For i As Integer = 0 to n : Print Using "###"; i; : Next
Print
Print String(78, "-")
Print "F :";
For i As Integer = 0 To n : Print Using "###"; F(i); : Next
Print
Print "M :";
For i As Integer = 0 To n : Print Using "###"; M(i); : Next
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,9 @@
mutual {
F : Nat -> Nat
F Z = (S Z)
F (S n) = (S n) `minus` M(F(n))
M : Nat -> Nat
M Z = Z
M (S n) = (S n) `minus` F(M(n))
}

View file

@ -0,0 +1,94 @@
'// LibreOffice Basic Implementation of Hofstadter Female-Male sequences
'// Utility functions
sub setfont(strfont)
ThisComponent.getCurrentController.getViewCursor.charFontName = strfont
end sub
sub newline
oVC = thisComponent.getCurrentController.getViewCursor
oText = oVC.text
oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
end sub
sub out(sString)
oVC = ThisComponent.getCurrentController.getViewCursor
oText = oVC.text
oText.insertString(oVC, sString, false)
end sub
sub outln(optional sString)
if not ismissing (sString) then out(sString)
newline
end sub
function intformat(n as integer,nlen as integer) as string
dim nstr as string
nstr = CStr(n)
while len(nstr) < nlen
nstr = " " & nstr
wend
intformat = nstr
end function
'// Hofstadter Female-Male function definitions
function F(n as long) as long
if n = 0 Then
F = 1
elseif n > 0 Then
F = n - M(F(n - 1))
endif
end function
function M(n)
if n = 0 Then
M = 0
elseif n > 0 Then
M = n - F(M(n - 1))
endif
end function
'// Hofstadter Female Male sequence demo routine
sub Hofstadter_Female_Male_Demo
'// Introductory Text
setfont("LM Roman 10")
outln("Rosetta Code Hofstadter Female and Male Sequence Challenge")
outln
out("Two functions are said to be mutually recursive if the first calls the second,")
outln(" and in turn the second calls the first.")
out("Write two mutually recursive functions that compute members of the Hofstadter")
outln(" Female and Male sequences defined as:")
outln
setfont("LM Mono Slanted 10")
outln(chr(9)+"F(0) = 1 ; M(0)=0")
outln(chr(9)+"F(n) = n - M(F(n-1)), n > 0")
outln(chr(9)+"M(n) = n - F(M(n-1)), n > 0")
outln
'// Sequence Generation
const nmax as long = 20
dim n as long
setfont("LM Mono 10")
out("n = "
for n = 0 to nmax
out(" " + intformat(n, 2))
next n
outln
out("F(n) = "
for n = 0 to nmax
out(" " + intformat(F(n),2))
next n
outln
out("M(n) = "
for n = 0 to nmax
out(" " + intformat(M(n), 2))
next n
outln
end sub
------------------------------
Output
------------------------------
n = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
F(n) = 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 13
M(n) = 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12

View file

@ -0,0 +1,13 @@
proc m(n): int
proc f(n): int =
if n == 0: 1
else: n - m(f(n-1))
proc m(n): int =
if n == 0: 0
else: n - f(m(n-1))
for i in 1 .. 10:
echo f(i)
echo m(i)

View file

@ -0,0 +1,12 @@
Method new: M
Integer method: F
self 0 == ifTrue: [ 1 return ]
self self 1 - F M - ;
Integer method: M
self 0 == ifTrue: [ 0 return ]
self self 1 - M F - ;
0 20 seqFrom map(#F) println
0 20 seqFrom map(#M) println

View file

@ -0,0 +1,15 @@
function F(integer n)
return iff(n?n-M(F(n-1)):1)
end function
function M(integer n)
return iff(n?n-F(M(n-1)):0)
end function
for i=0 to 20 do
printf(1," %d",F(i))
end for
printf(1,"\n")
for i=0 to 20 do
printf(1," %d",M(i))
end for

View file

@ -0,0 +1,2 @@
forward function F(integer n)
forward function M(integer n)

View file

@ -0,0 +1,19 @@
see "F sequence : "
for i = 0 to 20
see "" + f(i) + " "
next
see nl
see "M sequence : "
for i = 0 to 20
see "" + m(i) + " "
next
func f n
fr = 1
if n != 0 fr = n - m(f(n - 1)) ok
return fr
func m n
mr = 0
if n != 0 mr = n - f(m(n - 1)) ok
return mr

View file

@ -0,0 +1,9 @@
func F(){};
func M(){};
F = func(n) { n > 0 ? (n - M(F(n-1))) : 1 };
M = func(n) { n > 0 ? (n - F(M(n-1))) : 0 };
[F, M].each { |seq|
(0..19).map {|i| seq.call(i)}.join(' ').say;
}

View file

@ -0,0 +1,16 @@
func F(n: Int) -> Int {
return n == 0 ? 1 : n - M(F(n-1))
}
func M(n: Int) -> Int {
return n == 0 ? 0 : n - F(M(n-1))
}
for i in 0..20 {
print("\(F(i)) ")
}
println()
for i in 0..20 {
print("\(M(i)) ")
}
println()

View file

@ -0,0 +1,4 @@
def F: 0; # declare required signature
def M: if . == 0 then 0 else . - ((. - 1) | M | F) end;
def F: if . == 0 then 1 else . - ((. - 1) | F | M) end;

View file

@ -0,0 +1,2 @@
[range(0;20) | F],
[range(0;20) | M]

View file

@ -0,0 +1,4 @@
$ jq -n -c -f Mutual_recursion.jq
[1,1,2,2,3,3,4,5,5,6,6,7,8,8,9,9,10,11,11,12]
[0,0,1,2,2,3,4,4,5,6,6,7,7,8,9,9,10,11,11,12]