在这种情况下如何再次使用结果
How to use result again in this case
我的任务是:
Implement the function puzz
. It should simulate the game similar to 15 Puzzle. In our case, we have 25 squares, where 24 squares are occupied by tiles with big case letters from 'A'
to 'X'
. One tile is free and is denoted by ' '
. In each move, you can move a tile (denoted by its letter) into the free space. The function gets the original configuration and a sequence of valid moves. It should output the resulting configuration.
我是新手Haskell,我只能做第一步。
最后一步后的结果必须是 ["ABCDE", "FGHIJ", "KLMNO", "PQRST ", "UVWX_"]
。如何保存第一步的结果并用它来做第二步,然后下一步,然后再迭代一些?
s1 = ["AC DE",
"FBHIJ",
"KGLNO",
"PQMRS",
"UVWXT"]
p1 = "CBGLMRST"
puzz :: Result -> [Char] -> Result
puzz s1 p1 =
let i = 0
p = p1 !! i
change r = [ if c == ' ' then p else if c == p then ' ' else c | c <- r ]
out = [ change r | r <- s1 ]
in out
puzz s1 p1
中的 s1
和 p1
不是您的常量 s1
和 p1
。它们只是模板。例如 puzz [" B"] "B"
returns ["BB "]
和 p1
变成 [" B"]
并且 s1
在那个函数调用中变成 "B"
。
- 不要尝试迭代(我用
i
删除了部分代码)。使用递归。 Haskell里面没有变量,只有常量和输入参数。由于参照完整性,输入参数(由模板表示)在每次调用中都应该不同(接近尾声)。
- 别忘了完成算法。
(p:p1)
- Template of input parameter which says that there is a nonempty list and the first character will be p
and tail will be p1
.
puzz s1 [] = s1
- When second parametr is empty list, then return first parameter (end).
puzz out p1
- Calling puzz for the changed s1
as out
and p1
from (p:p1)
(recursion).
s1 = ["AC DE","FBHIJ","KGLNO","PQMRS","UVWXT"]
p1 = "CBGLMRST"
puzz :: [String] -> [Char]-> [String]
puzz s1 (p:p1) = let
change r = [ if c == ' ' then p else if c == p then ' ' else c | c <- r ]
out = [ change r | r <- s1 ]
in puzz out p1
puzz s1 [] = s1
输出:
puzz s1 p1
=> ["ABCDE","FGHIJ","KLMNO","PQRST","UVWX "]
我的任务是:
Implement the function
puzz
. It should simulate the game similar to 15 Puzzle. In our case, we have 25 squares, where 24 squares are occupied by tiles with big case letters from'A'
to'X'
. One tile is free and is denoted by' '
. In each move, you can move a tile (denoted by its letter) into the free space. The function gets the original configuration and a sequence of valid moves. It should output the resulting configuration.
我是新手Haskell,我只能做第一步。
最后一步后的结果必须是 ["ABCDE", "FGHIJ", "KLMNO", "PQRST ", "UVWX_"]
。如何保存第一步的结果并用它来做第二步,然后下一步,然后再迭代一些?
s1 = ["AC DE",
"FBHIJ",
"KGLNO",
"PQMRS",
"UVWXT"]
p1 = "CBGLMRST"
puzz :: Result -> [Char] -> Result
puzz s1 p1 =
let i = 0
p = p1 !! i
change r = [ if c == ' ' then p else if c == p then ' ' else c | c <- r ]
out = [ change r | r <- s1 ]
in out
puzz s1 p1
中的s1
和p1
不是您的常量s1
和p1
。它们只是模板。例如puzz [" B"] "B"
returns["BB "]
和p1
变成[" B"]
并且s1
在那个函数调用中变成"B"
。- 不要尝试迭代(我用
i
删除了部分代码)。使用递归。 Haskell里面没有变量,只有常量和输入参数。由于参照完整性,输入参数(由模板表示)在每次调用中都应该不同(接近尾声)。 - 别忘了完成算法。
(p:p1)
- Template of input parameter which says that there is a nonempty list and the first character will bep
and tail will bep1
.
puzz s1 [] = s1
- When second parametr is empty list, then return first parameter (end).
puzz out p1
- Calling puzz for the changeds1
asout
andp1
from(p:p1)
(recursion).
s1 = ["AC DE","FBHIJ","KGLNO","PQMRS","UVWXT"]
p1 = "CBGLMRST"
puzz :: [String] -> [Char]-> [String]
puzz s1 (p:p1) = let
change r = [ if c == ' ' then p else if c == p then ' ' else c | c <- r ]
out = [ change r | r <- s1 ]
in puzz out p1
puzz s1 [] = s1
输出:
puzz s1 p1
=> ["ABCDE","FGHIJ","KLMNO","PQRST","UVWX "]