Home Articles

函数声明中的非详尽模式

Asked
Viewed 1382 times
0

我在实现以下功能时遇到困难:

type Tabuleiro = [String]
type Comandos = String
type Comando = String
type Coordenadas = String

novaCoord :: Tabuleiro -> Comandos -> Coordenadas -> Coordenadas

novaCoord l (cmd:xs) coor 
                    | (c3 =="N") && cmd =='A' = [intToDigit(c1)   , ' ',intToDigit(c2+1) , ' ',c3!!0]
                    | (c3 =="E") && cmd =='A' = [intToDigit(c1+1) , ' ',intToDigit(c2)   , ' ',c3!!0]
                    | (c3 =="O") && cmd =='A' = [intToDigit(c1-1) , ' ',intToDigit(c2)   , ' ',c3!!0]
                    | (c3 =="S") && cmd =='A' = [intToDigit(c1)   , ' ',intToDigit(c2-1) , ' ',c3!!0]
                    | (c3 =="N") && cmd =='E' = [intToDigit(c1)   , ' ',intToDigit(c2) , ' ',"O"!!0]
                    | (c3 =="E") && cmd =='E' = [intToDigit(c1)   , ' ',intToDigit(c2) , ' ',"N"!!0]
                    | (c3 =="S") && cmd =='E' = [intToDigit(c1)   , ' ',intToDigit(c2) , ' ',"E"!!0]
                    | (c3 =="O") && cmd =='E' = [intToDigit(c1)   , ' ',intToDigit(c2) , ' ',"S"!!0]
                    | (c3 =="N") && cmd =='D' = [intToDigit(c1)   , ' ',intToDigit(c2) , ' ',"E"!!0]
                    | (c3 =="E") && cmd =='D' = [intToDigit(c1)   , ' ',intToDigit(c2) , ' ',"S"!!0]
                    | (c3 =="S") && cmd =='D' = [intToDigit(c1)   , ' ',intToDigit(c2) , ' ',"O"!!0]
                    | (c3 =="O") && cmd =='D' = [intToDigit(c1)   , ' ',intToDigit(c2) , ' ',"N"!!0]
                    | (c3 =="N") && cmd =='S' = [intToDigit(c1)   , ' ',intToDigit(c2+1) , ' ',c3!!0]
                    | (c3 =="E") && cmd =='S' = [intToDigit(c1+1) , ' ',intToDigit(c2)   , ' ',c3!!0]
                    | (c3 =="O") && cmd =='S' = [intToDigit(c1-1) , ' ',intToDigit(c2)   , ' ',c3!!0]
                    | (c3 =="S") && cmd =='S' = [intToDigit(c1)   , ' ',intToDigit(c2-1) , ' ',c3!!0]


                   where
                    ytotal=(length l)-1
                    coords=converte coor
                    (c1,c2,c3)=coords
                    y=l!!((ytotal)-(c2))
                    x=y!!(c1)

                    converte :: Coordenadas -> (Int,Int,String)
                    converte [] = (0,0,"S")
                    converte (x:y:z) = 
                                    let 
                                    coords= words (x:y:z)
                                    c = coords !! 2
                                    a=read (coords !! 0) :: Int  -- Coord X
                                    b=read (coords !! 1) :: Int  -- Coord Y
                                    in (a,b,c)

编译器抱怨说我没有详尽的模式 . 我试过了

novaCoord [] _ _ = ("0 0 S") 
novaCoord _ [] _ = ("0 0 S") 
novaCoord _ _ [] = ("0 0 S")

但没有运气!我感谢任何可以提供的帮助,通用或不通用!

1 Answer

  • 3

    你需要完成 novaCoord _ [] _ 的模式匹配,你需要在 novaCoord 的守卫中使用 otherwise 子句,而你在 novaCoord 的where子句中错过了 converte [x] 的情况 .

Related