2016-05-08 6 views
2

私はこのHaskellの関数にコンバートHaskellの機能

llenaPares _ [] = [] 
llenaPares (a,b) ((c,d):ys) 
     | a < c  = (a,b) : llenaPares (a+1,b) ((c,d):ys) 
     | otherwise = (c,d) : llenaPares (c+1,d) ys 

を変換しようとしている基本的にはこの機能が

[(1, 8), (5, 0), (6, 4), (10, 0), (11, 10), (12, 0)] 

、例えば、引数としてタプルのリストを受け取り、リストを返します各タプルペアが存在する場合はリストの0からmax(x)まで存在するか、以前の値。例

[(1,3),(3,5),(7,9),(12,0)] will return [(0,0),(1,3),(2,3),(3,5),(4,5),(5,5),(6x9),(7,9),(8,9),(9,9),(10,9),(11,0)] 

についてイムは、プロローグ

llenaPares(A,B,[], RES). 
llenaPares(A,B,[c(C,D)|YS], RES) :- 
     A < C, 
     append([c(A,B)], RES, SOL), 
     sum(A,1,SIG), 
     llenaPares(SIG,B,[c(C,D)|YS],SOL). 
llenaPares(A,B,[c(C,D)|YS], RES) :- 
    C>=A, 
    append([c(C,D)], RES, SOL), 
    sum(C,1,SIG), 
    llenaPares(SIG,D,YS, SOL). 

sum(A,B,C):-C is A+B. 

でこれをやってでも動作しません。 どうしたの?

?- 
| llenaPares(0,0,[c(1,8),c(5,0),c(6,4),c(10,0),c(11,10),c(12,0)], SAL). 
    Call: (7) llenaPares(0, 0, [c(1, 8), c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], _G4798) ? creep 
    Call: (8) 0<1 ? creep 
    Exit: (8) 0<1 ? creep 
    Call: (8) lists:append([c(0, 0)], _G4798, _G4922) ? creep 
    Exit: (8) lists:append([c(0, 0)], _G4798, [c(0, 0)|_G4798]) ? creep 
    Call: (8) sum(0, 1, _G4925) ? creep 
    Call: (9) _G4926 is 0+1 ? creep 
    Exit: (9) 1 is 0+1 ? creep 
    Exit: (8) sum(0, 1, 1) ? creep 
    Call: (8) llenaPares(1, 0, [c(1, 8), c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(0, 0)|_G4798]) ? creep 
    Call: (9) 1<1 ? creep 
    Fail: (9) 1<1 ? creep 
    Redo: (8) llenaPares(1, 0, [c(1, 8), c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(0, 0)|_G4798]) ? creep 
    Call: (9) 1>=1 ? creep 
    Exit: (9) 1>=1 ? creep 
    Call: (9) lists:append([c(1, 8)], [c(0, 0)|_G4798], _G4940) ? creep 
    Exit: (9) lists:append([c(1, 8)], [c(0, 0)|_G4798], [c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (9) sum(1, 1, _G4943) ? creep 
    Call: (10) _G4944 is 1+1 ? creep 
    Exit: (10) 2 is 1+1 ? creep 
    Exit: (9) sum(1, 1, 2) ? creep 
    Call: (9) llenaPares(2, _G4945, [c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (10) 2<5 ? creep 
    Exit: (10) 2<5 ? creep 
    Call: (10) lists:append([c(2, _G4941)], [c(1, 8), c(0, 0)|_G4798], _G4952) ? creep 
    Exit: (10) lists:append([c(2, _G4941)], [c(1, 8), c(0, 0)|_G4798], [c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (10) sum(2, 1, _G4955) ? creep 
    Call: (11) _G4956 is 2+1 ? creep 
    Exit: (11) 3 is 2+1 ? creep 
    Exit: (10) sum(2, 1, 3) ? creep 
    Call: (10) llenaPares(3, _G4941, [c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (11) 3<5 ? creep 
    Exit: (11) 3<5 ? creep 
    Call: (11) lists:append([c(3, _G4941)], [c(2, _G4941), c(1, 8), c(0, 0)|_G4798], _G4970) ? creep 
    Exit: (11) lists:append([c(3, _G4941)], [c(2, _G4941), c(1, 8), c(0, 0)|_G4798], [c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (11) sum(3, 1, _G4973) ? creep 
    Call: (12) _G4974 is 3+1 ? creep 
    Exit: (12) 4 is 3+1 ? creep 
    Exit: (11) sum(3, 1, 4) ? creep 
    Call: (11) llenaPares(4, _G4941, [c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (12) 4<5 ? creep 
    Exit: (12) 4<5 ? creep 
    Call: (12) lists:append([c(4, _G4941)], [c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798], _G4988) ? creep 
    Exit: (12) lists:append([c(4, _G4941)], [c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798], [c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (12) sum(4, 1, _G4991) ? creep 
    Call: (13) _G4992 is 4+1 ? creep 
    Exit: (13) 5 is 4+1 ? creep 
    Exit: (12) sum(4, 1, 5) ? creep 
    Call: (12) llenaPares(5, _G4941, [c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (13) 5<5 ? creep 
    Fail: (13) 5<5 ? creep 
    Redo: (12) llenaPares(5, _G4941, [c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (13) 5>=5 ? creep 
    Exit: (13) 5>=5 ? creep 
    Call: (13) lists:append([c(5, 0)], [c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798], _G5006) ? creep 
    Exit: (13) lists:append([c(5, 0)], [c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798], [c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (13) sum(5, 1, _G5009) ? creep 
    Call: (14) _G5010 is 5+1 ? creep 
    Exit: (14) 6 is 5+1 ? creep 
    Exit: (13) sum(5, 1, 6) ? creep 
    Call: (13) llenaPares(6, _G5011, [c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (14) 6<6 ? creep 
    Fail: (14) 6<6 ? creep 
    Redo: (13) llenaPares(6, _G5011, [c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (14) 6>=6 ? creep 
    Exit: (14) 6>=6 ? creep 
    Call: (14) lists:append([c(6, 4)], [c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798], _G5018) ? creep 
    Exit: (14) lists:append([c(6, 4)], [c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798], [c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(..., ...)|...]) ? creep 
    Call: (14) sum(6, 1, _G5021) ? creep 
    Call: (15) _G5022 is 6+1 ? creep 
    Exit: (15) 7 is 6+1 ? creep 
    Exit: (14) sum(6, 1, 7) ? creep 
    Call: (14) llenaPares(7, _G5023, [c(10, 0), c(11, 10), c(12, 0)], [c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Call: (15) 7<10 ? creep 
    Exit: (15) 7<10 ? creep 
    Call: (15) lists:append([c(7, _G5019)], [c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(..., ...)|...], _G5030) ? creep 
    Exit: (15) lists:append([c(7, _G5019)], [c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(..., ...)|...], [c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(..., ...)|...]) ? creep 
    Call: (15) sum(7, 1, _G5033) ? creep 
    Call: (16) _G5034 is 7+1 ? creep 
    Exit: (16) 8 is 7+1 ? creep 
    Exit: (15) sum(7, 1, 8) ? creep 
    Call: (15) llenaPares(8, _G5019, [c(10, 0), c(11, 10), c(12, 0)], [c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(..., ...)|...]) ? creep 
    Call: (16) 8<10 ? creep 
    Exit: (16) 8<10 ? creep 
    Call: (16) lists:append([c(8, _G5019)], [c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(..., ...)|...], _G5048) ? creep 
    Exit: (16) lists:append([c(8, _G5019)], [c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(..., ...)|...], [c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(..., ...)|...]) ? creep 
    Call: (16) sum(8, 1, _G5051) ? creep 
    Call: (17) _G5052 is 8+1 ? creep 
    Exit: (17) 9 is 8+1 ? creep 
    Exit: (16) sum(8, 1, 9) ? creep 
    Call: (16) llenaPares(9, _G5019, [c(10, 0), c(11, 10), c(12, 0)], [c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(..., ...)|...]) ? creep 
    Call: (17) 9<10 ? creep 
    Exit: (17) 9<10 ? creep 
    Call: (17) lists:append([c(9, _G5019)], [c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(..., ...)|...], _G5066) ? creep 
    Exit: (17) lists:append([c(9, _G5019)], [c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(..., ...)|...], [c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(..., ...)|...]) ? creep 
    Call: (17) sum(9, 1, _G5069) ? creep 
    Call: (18) _G5070 is 9+1 ? creep 
    Exit: (18) 10 is 9+1 ? creep 
    Exit: (17) sum(9, 1, 10) ? creep 
    Call: (17) llenaPares(10, _G5019, [c(10, 0), c(11, 10), c(12, 0)], [c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(..., ...)|...]) ? creep 
    Call: (18) 10<10 ? creep 
    Fail: (18) 10<10 ? creep 
    Redo: (17) llenaPares(10, _G5019, [c(10, 0), c(11, 10), c(12, 0)], [c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(..., ...)|...]) ? creep 
    Call: (18) 10>=10 ? creep 
    Exit: (18) 10>=10 ? creep 
    Call: (18) lists:append([c(10, 0)], [c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(..., ...)|...], _G5084) ? creep 
    Exit: (18) lists:append([c(10, 0)], [c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(..., ...)|...], [c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(..., ...)|...]) ? creep 
    Call: (18) sum(10, 1, _G5087) ? creep 
    Call: (19) _G5088 is 10+1 ? creep 
    Exit: (19) 11 is 10+1 ? creep 
    Exit: (18) sum(10, 1, 11) ? creep 
    Call: (18) llenaPares(11, _G5089, [c(11, 10), c(12, 0)], [c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(..., ...)|...]) ? creep 
    Call: (19) 11<11 ? creep 
    Fail: (19) 11<11 ? creep 
    Redo: (18) llenaPares(11, _G5089, [c(11, 10), c(12, 0)], [c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(..., ...)|...]) ? creep 
    Call: (19) 11>=11 ? creep 
    Exit: (19) 11>=11 ? creep 
    Call: (19) lists:append([c(11, 10)], [c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(..., ...)|...], _G5096) ? creep 
    Exit: (19) lists:append([c(11, 10)], [c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(..., ...)|...], [c(11, 10), c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(..., ...)|...]) ? creep 
    Call: (19) sum(11, 1, _G5099) ? creep 
    Call: (20) _G5100 is 11+1 ? creep 
    Exit: (20) 12 is 11+1 ? creep 
    Exit: (19) sum(11, 1, 12) ? creep 
    Call: (19) llenaPares(12, _G5101, [c(12, 0)], [c(11, 10), c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(..., ...)|...]) ? creep 
    Call: (20) 12<12 ? creep 
    Fail: (20) 12<12 ? creep 
    Redo: (19) llenaPares(12, _G5101, [c(12, 0)], [c(11, 10), c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(..., ...)|...]) ? creep 
    Call: (20) 12>=12 ? creep 
    Exit: (20) 12>=12 ? creep 
    Call: (20) lists:append([c(12, 0)], [c(11, 10), c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(..., ...)|...], _G5108) ? creep 
    Exit: (20) lists:append([c(12, 0)], [c(11, 10), c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(..., ...)|...], [c(12, 0), c(11, 10), c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(..., ...)|...]) ? creep 
    Call: (20) sum(12, 1, _G5111) ? creep 
    Call: (21) _G5112 is 12+1 ? creep 
    Exit: (21) 13 is 12+1 ? creep 
    Exit: (20) sum(12, 1, 13) ? creep 
    Call: (20) llenaPares(13, _G5113, [], [c(12, 0), c(11, 10), c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(..., ...)|...]) ? creep 
    Exit: (20) llenaPares(13, _G5113, [], [c(12, 0), c(11, 10), c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(..., ...)|...]) ? creep 
    Exit: (19) llenaPares(12, _G5113, [c(12, 0)], [c(11, 10), c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(..., ...)|...]) ? creep 
    Exit: (18) llenaPares(11, _G5113, [c(11, 10), c(12, 0)], [c(10, 0), c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(..., ...)|...]) ? creep 
    Exit: (17) llenaPares(10, _G5019, [c(10, 0), c(11, 10), c(12, 0)], [c(9, _G5019), c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(..., ...)|...]) ? creep 
    Exit: (16) llenaPares(9, _G5019, [c(10, 0), c(11, 10), c(12, 0)], [c(8, _G5019), c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(..., ...)|...]) ? creep 
    Exit: (15) llenaPares(8, _G5019, [c(10, 0), c(11, 10), c(12, 0)], [c(7, _G5019), c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(..., ...)|...]) ? creep 
    Exit: (14) llenaPares(7, _G5019, [c(10, 0), c(11, 10), c(12, 0)], [c(6, 4), c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Exit: (13) llenaPares(6, _G5113, [c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(5, 0), c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Exit: (12) llenaPares(5, _G4941, [c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(4, _G4941), c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Exit: (11) llenaPares(4, _G4941, [c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(3, _G4941), c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Exit: (10) llenaPares(3, _G4941, [c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(2, _G4941), c(1, 8), c(0, 0)|_G4798]) ? creep 
    Exit: (9) llenaPares(2, _G4941, [c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(1, 8), c(0, 0)|_G4798]) ? creep 
    Exit: (8) llenaPares(1, 0, [c(1, 8), c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], [c(0, 0)|_G4798]) ? creep 
    Exit: (7) llenaPares(0, 0, [c(1, 8), c(5, 0), c(6, 4), c(10, 0), c(11, 10), c(12, 0)], _G4798) ? creep 
true . 
+2

をさて、あなたは何を期待していますか?成功するか失敗するかを教えてください。 – false

+0

私の質問では、入力と出力の例を書きました。私はそれを期待しています。 – colymore

+3

あなたが試した**具体的な目標を入れてください。そうでなければ、これは推測のみです – false

答えて

1

私のプロローグは少し錆びですが、ここで私が見たものである:中

llenaPares(A,B,[], RES). 
        ^^^^ shouldn't this be []? 

llenaPares(A,B,[c(C,D)|YS], RES) :- 
     A < C, 
     append([c(A,B)], RES, SOL), 
     sum(A,1,SIG), 
     llenaPares(SIG,B,[c(C,D)|YS],SOL). 

はRES SOLより大きくリストすべきではありませんか?私はあなたがappendコールでそれらを交換する必要があると思います。

最後に、C >= A(これはA < Cとほぼ同じです)の代わりにA >= Cと思っています。

+3

ここで 'append/3'はまったく必要ありません。 Lsは最初の要素が 'c(A、B)'であり、残りが 'Ls0'であるリストであることを示すために' Ls = [c(A、B)| Ls0] 'のような目標を書くだけです。より一般的には、既知の要素*をリストの先頭に追加するために 'append/3'を使う必要はありません。前と同じように、統一を使って直接書き留めてください。 – mat

3

できるだけ忠実にあなたのプログラムをエミュレートする:

:- use_module(library(clpfd)). 

llenaPares(_, [], []). 
llenaPares((A,B), [(C,D)|Ys], [P|Ps]) :- 
     A #< C, P = (A,B), Ap #= A+1, llenaPares((Ap,B), [(C,D)|Ys], Ps) 
     ; A #>= C, P = (C,D), Cp #= C+1, llenaPares((Cp,D), Ys, Ps). 

これはSICStusまたはSWIで動作します。今、私たちは「入力を計算する」ために関係を使用することができます。

?- llenaPares(P, Ys, [(0,0),(1,8),(2,8),(3,8),(4,8),(5,0),(6,4),(7,4),(8,4),(9,4),(10,0),(11,10),(12,0)]). 
    P = (0,0), 
    Ys = [(1,8),(5,0),(6,4),(10,0),(11,10),(12,0)] 
; P = (0,0), 
    Ys = [(1,8),(5,0),(6,4),(9,4),(10,0),(11,10),(12,0)] 
; P = (0,0), 
    Ys = [(1,8),(5,0),(6,4),(8,4),(10,0),(11,10),(12,0)] 
; ... 

だからここに「結果」が は、その結果につながることをYs与えられた、と私たちはすべての「入力」をお願いしています。

それとも我々は単にこの関係が何であるか尋ねることがあります。

?- length(Ps, N), llenaPares(P, Ys, Ps). 
    Ps = [], 
    N = 0, 
    Ys = [] 
; Ps = [(_A,_B)], 
    N = 1, 
    P = (_C,_D), Ys = [(_A,_B)], 
    _E#=_A+1, _A#=<_C, _A in inf..sup, _C in inf..sup, _E in inf..sup 
; Ps = [(_A,_B),(_C,_D)], 
    N = 2, 
    P = (_A,_B), 
    Ys = [(_C,_D)], 
    _E#=_A+1, _F#=_C+1, _C#=<_E, _C#>=_A+1, _C in inf..sup, _A in inf..sup, _E in inf..sup,_F in inf..sup 
; ...