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

50
Task/Amb/ERRE/amb.erre Normal file
View file

@ -0,0 +1,50 @@
PROGRAM AMB
!
! for rosettacode.org
!
!$KEY
DIM SET1$[2],SET2$[2],SET3$[2],SET4$[2]
FUNCTION WORDS_OK(STRING1$,STRING2$)
WORDS_OK=(RIGHT$(STRING1$,1)=LEFT$(STRING2$,1))
END FUNCTION
PROCEDURE AMB(SET1$[],SET2$[],SET3$[],SET4$[]->RESULT$)
RESULT$="" ! Empty string, e.g. fail
FOR A=0 TO 2 DO
FOR B=0 TO 2 DO
FOR C=0 TO 2 DO
FOR D=0 TO 2 DO
IF WORDS_OK(SET1$[A],SET2$[B]) AND WORDS_OK(SET2$[B],SET3$[C]) AND WORDS_OK(SET3$[C],SET4$[D]) THEN
RESULT$=SET1$[A]+" "+SET2$[B]+" "+SET3$[C]+" "+SET4$[D]
EXIT PROCEDURE
END IF
END FOR
END FOR
END FOR
END FOR
END PROCEDURE
BEGIN
PRINT(CHR$(12);) ! CLS
SET1$[0]="the" SET1$[1]="that" SET1$[2]="a"
SET2$[0]="frog" SET2$[1]="elephant" SET2$[2]="thing"
SET3$[0]="walked" SET3$[1]="treaded" SET3$[2]="grows"
SET4$[0]="slowly" SET4$[1]="quickly" SET4$[2]=""
AMB(SET1$[],SET2$[],SET3$[],SET4$[]->TEXT$)
IF TEXT$<>"" THEN
PRINT("Correct sentence would be:")
PRINT(TEXT$)
ELSE
PRINT("Failed to fine a correct sentence.")
END IF
PRINT
PRINT("Press any key to exit.")
REPEAT
GET(Z$)
UNTIL LEN(Z$)<>0
END PROGRAM

View file

@ -0,0 +1,7 @@
; We don't need 'amb' in the code since pattern-matching of Egison automatically do backtracking.
(match-all {{"the" "that" "a"} {"frog" "elephant" "thing"} {"walked" "treaded" "grows"} {"slowly" "quickly"}} (list (multiset string))
[<cons <cons (& <snoc $c_1 _> $w_1) _>
(loop $i [2 $n]
<cons <cons (& <cons ,c_(- i 1) <snoc $c_i _>> $w_i) _> ...>
<nil>)>
(map (lambda [$i] w_i) (between 1 n))])

View file

@ -0,0 +1 @@
{{"that" "thing" "grows" "slowly"}}

31
Task/Amb/Nim/amb.nim Normal file
View file

@ -0,0 +1,31 @@
import future, strutils
proc amb(comp: proc(a, b: string): bool, options: seq[seq[string]],
prev: string = nil): seq[string] =
if options.len == 0: return @[]
for opt in options[0]:
# If this is the base call, prev is nil and we need to continue.
if prev != nil and not comp(prev, opt): continue
# Take care of the case where we have no options left.
if options.len == 1: return @[opt]
# Traverse into the tree.
let res = amb(comp, options[1..options.high], opt)
# If it was a failure, try the next one.
if res.len > 0: return opt & res # We have a match
return @[]
const sets = @[@["the", "that", "a"],
@["frog", "elephant", "thing"],
@["walked", "treaded", "grows"],
@["slowly", "quickly"]]
let result = amb((s, t: string) => (s[s.high] == t[0]), sets)
if result.len == 0:
echo "No matches found!"
else:
echo result.join " "

38
Task/Amb/Phix/amb-1.phix Normal file
View file

@ -0,0 +1,38 @@
function amb1(sequence sets, object res=0, integer idx=1)
integer ch = 0
integer pass = 0
if idx>length(sets) then
pass = 1
else
if res=0 then
res = repeat(0,length(sets))
else
ch = sets[idx-1][res[idx-1]][$]
end if
for k=1 to length(sets[idx]) do
if ch=0 or sets[idx][k][1]=ch then
res[idx] = k
{pass,res} = amb1(sets,res,idx+1)
if pass then exit end if
end if
end for
end if
return {pass,res}
end function
sequence sets = {{"the","that","a"},
{"frog","elephant","thing"},
{"walked","treaded","grows"},
{"slowly","quickly"}}
integer pass
sequence res
{pass,res} = amb1(sets)
if pass then
puts(1,"success: ")
for i=1 to length(sets) do
res[i] = sets[i][res[i]]
end for
?res
else
puts(1,"failure\n")
end if

82
Task/Amb/Phix/amb-2.phix Normal file
View file

@ -0,0 +1,82 @@
function amb(sequence sets, integer testrid, integer resrid=-1, object res=0, integer idx=1)
integer flag = (res==0)
integer pass = 0
if idx>length(sets) then
pass = 1
if resrid!=-1 then
call_proc(resrid,{sets,res})
end if
else
if flag then
res = repeat(0,length(sets))
end if
for k=1 to length(sets[idx]) do
res[idx] = k
if flag or call_func(testrid,{sets,res,idx}) then
{pass,res} = amb(sets,testrid,resrid,res,idx+1)
if pass and resrid=-1 then exit end if
end if
end for
end if
return {pass,res}
end function
function pairable(sequence sets, sequence res, integer idx)
return sets[idx-1][res[idx-1]][$] = sets[idx][res[idx]][1]
end function
constant r_pairable = routine_id("pairable")
procedure AMB_Show(sequence sets, sequence res)
puts(1,"success: ")
for i=1 to length(sets) do
res[i] = sets[i][res[i]]
end for
?res
end procedure
constant r_show = routine_id("AMB_Show")
function pythagorean(sequence sets, sequence res, integer idx)
-- (note that res[idx]==sets[idx][res[idx]] in all cases)
integer x, y, z
if sequence(sets) then end if -- (suppress warning)
{x,y,z} = res
return idx<3 or (x*x+y*y=z*z)
end function
constant r_pythagorean = routine_id("pythagorean")
procedure pythag_show(sequence sets, sequence res)
if sequence(sets) then end if -- (suppress warning)
puts(1,"success: ")
?res
end procedure
constant r_pythag_show = routine_id("pythag_show")
-- see http://www.randomhacks.net/articles/2005/10/11/amb-operator
function not8(sequence sets, sequence res, integer idx)
-- (note that idx==2 in all cases)
-- (at the last moment, I flipped the test, after realising that
-- someone had completely misunderstood the original article...
-- ...and proved it by showing some strange output on rosetta.)
-- return sets[1][res[1]]*sets[idx][res[idx]]!=8
return sets[1][res[1]]*sets[idx][res[idx]]=8
end function
constant r_not8 = routine_id("not8")
procedure not8_show(sequence sets, sequence res)
puts(1,"success: ")
?{sets[1][res[1]],sets[2][res[2]]}
end procedure
constant r_not8_show = routine_id("not8_show")
sequence sets = {{"the","that","a"},
{"frog","elephant","thing"},
{"walked","treaded","grows"},
{"slowly","quickly"}}
sequence sets2 = repeat(tagset(11),3)
sequence sets3 = {{1, 2, 3}, {4, 5, 6}}
puts(1,"\nThe original:\n")
{} = amb(sets,r_pairable,r_show)
puts(1,"\nSmall Pythagorean triples problem:\n")
{} = amb(sets2,r_pythagorean,r_pythag_show)
puts(1,"\nSome strange not 8 problem:\n") -- (now fixed)
{} = amb(sets3,r_not8,r_not8_show)

6
Task/Amb/jq/amb-1.jq Normal file
View file

@ -0,0 +1,6 @@
def amb: .[];
def joins:
(.[0][-1:]) as $left
| (.[1][0:1]) as $right
| if $left == $right then true else empty end;

8
Task/Amb/jq/amb-2.jq Normal file
View file

@ -0,0 +1,8 @@
(["the","that","a"] | amb) as $word1
| (["frog","elephant","thing"] | amb) as $word2
| [$word1, $word2] | joins
| (["walked","treaded","grows"] | amb) as $word3
| [$word2, $word3] | joins
| (["slowly","quickly"] | amb) as $word4
| [$word3, $word4] | joins
| [$word1, $word2, $word3, $word4]

7
Task/Amb/jq/amb-3.jq Normal file
View file

@ -0,0 +1,7 @@
jq -n -f amb.jq
[
"that",
"thing",
"grows",
"slowly"
]

6
Task/Amb/jq/amb-4.jq Normal file
View file

@ -0,0 +1,6 @@
def amb(condition): .[] | select(condition);
def joins:
(.[0][-1:]) as $left
| (.[1][0:1]) as $right
| $left == $right ;

5
Task/Amb/jq/amb-5.jq Normal file
View file

@ -0,0 +1,5 @@
(["the","that","a"] | amb(true)) as $word1
| (["frog","elephant","thing"] | amb( [$word1, .] | joins)) as $word2
| (["walked","treaded","grows"] | amb( [$word2, .] | joins)) as $word3
| (["slowly","quickly"] | amb( [$word3, .] | joins)) as $word4
| [$word1, $word2,$word3, $word4]