如何从“多态数据类型a”转到Show类的实例(不使用“派生显示”)

时间:2012-11-29 21:36:41

标签: haskell

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

但我希望有一个带有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

欢迎所有帮助。

1 个答案:

答案 0 :(得分: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