1

在此文本下方,您可以看到一些用于创建 Arukone 谜题的代码。但是有一个问题:多态数据类型Puzzle a代表了一个带有 type 标签的 Arukone 谜题a

但我想有一个带有 Show 类实例的拼图类型(不使用deriving Show.) show 函数就足够了,showsPrec 不是必需的。

那么你知道这是如何工作的吗?我想为a以下类的实例的所有类型定义 Show ToChar

class ToChar a where
 toChar :: a -> Char

instance ToChar Char where
 toChar = id

instance ToChar Int where
 toChar = head . show

代码 :

import Data.Maybe (listToMaybe)

data Size  = Size Int Int deriving (Eq, Show)
data Pos   = Pos  Int Int deriving (Eq, Show)
data Link l = Link l Pos Pos
data Puzzle l = Puzzle Size [Link l]

assign :: Int -> a -> [a] -> [a]
assign 0 v (_:vs) = v:vs
assign n v (v’:vs) = v’: assign (n - 1) v vs

assign2 :: Int -> Int -> a -> [[a]] -> [[a]]
assign2 r c v vs = assign r (assign c v (vs !! r)) vs

assignPos :: Pos -> a -> [[a]] -> [[a]]
assignPos (Pos r c) = assign2 (r-1) (c-1)

tabulate :: Int -> (Int -> a) -> [a]
tabulate 0 _ = []
tabulate i f = f 0 : tabulate (i - 1) (f . (+1))

tabulate2 :: Int -> Int -> (Int -> Int -> a) -> [[a]]
tabulate2 b h f = tabulate h (\r -> tabulate b (\c -> f r c))

tabulatePos :: Size -> (Pos -> a) -> [[a]]
tabulatePos (Size h b) f = tabulate2 b h (\r c -> f (Pos (r +1) (c + 1)))

showPuzzle :: Puzzle a -> [[Maybe a]]
showPuzzle (Puzzle sz links) = tabulatePos sz findPosMaybe
  where findPosMaybe pos =  -- edit: findPosMaybe needs to be further left than the next line
          listToMaybe [l | Link l pos1 pos2 <- links, pos1 == pos || pos2 == pos]

解决方案和输入

*Main> show (Puzzle (Size 2 3) [Link 1 (Pos 1 1) (Pos 1 3), Link 2 (Pos 2 1) (Pos 2 3)])

"1 1\n2 2"


*Main> show (Puzzel (Size 5 5) [Link ’a’ (Pos 3 1) (Pos 4 3), Link ’b’ (Pos 5 1) (Pos 1 5), Link ’c’ (Pos 2 5) (Pos 5 5), Link ’d’ (Pos 4 1) (Pos 2 2)])

"    b\n d  c\na    \nd a  \nb   c"          <-- result -- remarks the spaces

最后一个例子的 putStrLn

    b
 d  c
a 
d a 
b   c

欢迎所有帮助。

4

1 回答 1

2

首先让我们使用ToChar该类将 Maybe 元素转换为空格或值:

maybeToString :: ToChar a => Maybe a -> String
maybeToString Nothing = " "
maybeToString (Just x) = toChar x : ""

现在我们可以制作 Show 实例:

instance ToChar a => Show (Puzzle a) where
  show = unlines . map (concatMap maybeToString) . showPuzzle

我不清楚你是否想要填充,所以你可以改变你的 MaybeToString 来说明" "toChar x : " "是否想要更多的空间。

通过上面的定义,我们得到:

*Main> show (Puzzle (Size 2 3) [Link (1 :: Int) (Pos 1 1) (Pos 1 3), Link 2 (Pos 2 1) (Pos 2 3)]) 
"1 1\n2 2\n"
*Main> putStrLn $ show (Puzzle (Size 2 3) [Link (1 :: Int) (Pos 1 1) (Pos 1 3), Link 2 (Pos 2 1) (Pos 2 3)]) 
1 1
2 2

示例 2:

*Main> show (Puzzle (Size 5 5) [Link 'a' (Pos 3 1) (Pos 4 3), Link 'b' (Pos 5 1) (Pos 1 5), Link 'c' (Pos 2 5) (Pos 5 5), Link 'd' (Pos 4 1) (Pos 2 2)])
"    b\n d  c\na    \nd a  \nb   c\n"
*Main> putStr it
    b
 d  c
a    
d a  
b   c
于 2012-11-29T21:55:08.457 回答