我似乎无法弄清楚一些Haskell错误

时间:2017-11-14 21:43:43

标签: haskell ghc

我正在尝试使用haskell进行一次俄罗斯方块游戏。

现在我正在尝试从给定输入中仅打印出一块电路板。

我遇到了一些错误。我的代码是基于我的教授给出的一个例子,它有点类似。

错误

assign3.hs:20:5: error:
The type signature for ‘leftToRight’ lacks an accompanying binding
|
20 |     leftToRight :: Int -> Board -> String -> [Char]    |     
^^^^^^^^^^^

我的代码

module Main where
import Text.Printf (printf)
import Test.HUnit
import Data.Set (empty,member,delete,insert,Set)
main = do
     input <- getContents
     putStr $ show $ readFrom input

class Disp a where
     disp :: a -> String
data Square = Empty deriving (Show, Read, Eq)
instance Disp Square where
     disp Empty = " "
type BoardCell = (Square,Int)
type Board = [BoardCell]
data Game = Game Board Int
instance Show Game where
     show(Game board width)=
         printBoard width board $ lefToRight width board
leftToRight :: Int -> Board -> String -> String
lefToRight width [] = ""
lefToRight width board = (lefToRight width $ drop width board) ++ ("|" ++ formatRow $ take width board ++ "|\n")
rowBreak :: a -> String
rowBreak row =
     foldr (\x -> \y -> "-" ++ y) "+\n" row
formatRow :: Board -> String
formatRow row =
     foldr (\x -> \y -> "" ++ row1 x ++ y) "" row
row1 :: BoardCell -> String
row1 (square,int) = disp square
printBoard :: Int -> Board -> String -> String
printBoard width [] string = string ++ rowBreak(replicate width 0)
printBoard width [(_,_)] string = printBoard width [] string
printBoard width (_:rest) string = printBoard width rest string

emptyBoard = Game [] 1
readFrom = foldr doLine emptyBoard . lines
doLine game = doCommand game . words

doCommand :: Game -> [String] -> Game
doCommand (Game _ width) ["board",cols,rows] =
             Game (take (int rows*width) $ zip (cycle [Empty]) (iterate succ 1))
                 width
                     where width = int cols

int :: String -> Int
int = read

我要做的是接受像“board 7 4”这样的输入并得到像这样的输出

          |       |
          |       |
          |       |
          |       |
          +-------+ 

1 个答案:

答案 0 :(得分:0)

正如Daniel Wagner在问题评论中指出的那样,你有一个拼写错误。 std::optional用于类型签名,拼写正确。 leftToRight用于函数定义,但拼写错误。

因此,类型签名没有完全相同名称的函数定义,这会给出你得到的错误。