List 如何使用';UNWORD&x27;在哈斯克尔?

List 如何使用';UNWORD&x27;在哈斯克尔?,list,haskell,pretty-print,List,Haskell,Pretty Print,我想打印如下所示的字符串列表 |Name|Country|Age| ------------------ |1 |USA |20 | |2 |UK |19 | 我能够通过以下方法实现这一点 printfieldName :: [String] -> String printfieldName [] = [] printfieldName (x:xs) = "|" ++ x ++ "\t" ++ printfieldName (xs) 是否可以使用内置函数“unw

我想打印如下所示的字符串列表

|Name|Country|Age|
------------------
|1   |USA    |20 |
|2   |UK     |19 |
我能够通过以下方法实现这一点

printfieldName :: [String] -> String
printfieldName [] = []
printfieldName (x:xs)  = "|" ++ x ++ "\t" ++ printfieldName (xs)

是否可以使用内置函数“unwords”来实现这一点。我可以用“unwords”打印它,但无法在单词之间放置

首先,我要这样写:

printfieldName []     = []
printfieldName (x:xs) = "|" ++ x ++ "\t" ++ printfieldName xs
concatMap (\x -> '|' : x ++ "\t")
嗯,实际上,不,像这样:

printfieldName []     = []
printfieldName (x:xs) = "|" ++ x ++ "\t" ++ printfieldName xs
concatMap (\x -> '|' : x ++ "\t")
嗯,也许更像:

concatMap (printf "|%s\t")
嗯。那么,它可以作为“unwords”来完成吗

-- | 'unwords' is an inverse operation to 'words'.
-- It joins words with separating spaces.
unwords                 :: [String] -> String
unwords []              =  ""
unwords ws              =  foldr1 (\w s -> w ++ ' ':s) ws

没有。但是看看你是否能把concatMap写成foldr…

首先,我会这样写:

printfieldName []     = []
printfieldName (x:xs) = "|" ++ x ++ "\t" ++ printfieldName xs
concatMap (\x -> '|' : x ++ "\t")
嗯,实际上,不,像这样:

printfieldName []     = []
printfieldName (x:xs) = "|" ++ x ++ "\t" ++ printfieldName xs
concatMap (\x -> '|' : x ++ "\t")
嗯,也许更像:

concatMap (printf "|%s\t")
嗯。那么,它可以作为“unwords”来完成吗

-- | 'unwords' is an inverse operation to 'words'.
-- It joins words with separating spaces.
unwords                 :: [String] -> String
unwords []              =  ""
unwords ws              =  foldr1 (\w s -> w ++ ' ':s) ws

不可以。但是看看你是否可以把concatMap写成foldr…

我看到在“|”和word之间有一个额外的空格,所以你可以使用这个函数:

printfieldName x = unwords (map ((++) "|") x)  ++ "|"
小解释:

(++) "|" - creates a function which take prefixes each word with "|", so
(++) "|" "test" -> "|test" 
然后,
map
将此函数应用于将其转换为
[“|1”、“|USA”、“|20”、…]的单词列表。


然后
unwords
将它们连接成一个字符串,单词之间留有空格。添加final

需要
++“|”
,我看到“|”和word之间有一个额外的空格,因此您可以使用此函数:

printfieldName x = unwords (map ((++) "|") x)  ++ "|"
小解释:

(++) "|" - creates a function which take prefixes each word with "|", so
(++) "|" "test" -> "|test" 
然后,
map
将此函数应用于将其转换为
[“|1”、“|USA”、“|20”、…]的单词列表。


然后
unwords
将它们连接成一个字符串,单词之间留有空格。添加最终的

数据需要
++“|”
。List
有一个名为interspere的函数。也许你可以用这个

printfieldName xs = "|" ++ unwords (intersperse "|\t" xs) ++ "|"

Data.List
有一个名为interspose的函数。也许你可以用这个

printfieldName xs = "|" ++ unwords (intersperse "|\t" xs) ++ "|"

也许你的要求有点过头了,但是:

formatTable :: [String] -> [[String]] -> String
formatTable header rows =
    formatRow header ++ dashRow ++ concatMap formatRow rows

    where formatRow cells = bracket '|' (spread cells)
          dashRow         = bracket '+' (map (\n -> replicate n '-') widths)
          bracket c cells = concatMap (c:) cells ++ (c:"\n")

          spread cells    = zipWith pad widths cells
          pad n s         = take n (s ++ repeat ' ')

          widths = foldr maxLengths (repeat 0) (header : rows)
          maxLengths = zipWith (\c l -> max (length c) l)
然后,例如:

> let h = words "Name Country Age"
> let rows = map words ["1 USA 20", "2 UK 19"]
> h
["Name","Country","Age"]
> rows
[["1","USA","20"],["2","UK","19"]]
> putStr $ formatTable h rows
|Name|Country|Age|
+----+-------+---+
|1   |USA    |20 |
|2   |UK     |19 |

也许你的要求有点过头了,但是:

formatTable :: [String] -> [[String]] -> String
formatTable header rows =
    formatRow header ++ dashRow ++ concatMap formatRow rows

    where formatRow cells = bracket '|' (spread cells)
          dashRow         = bracket '+' (map (\n -> replicate n '-') widths)
          bracket c cells = concatMap (c:) cells ++ (c:"\n")

          spread cells    = zipWith pad widths cells
          pad n s         = take n (s ++ repeat ' ')

          widths = foldr maxLengths (repeat 0) (header : rows)
          maxLengths = zipWith (\c l -> max (length c) l)
然后,例如:

> let h = words "Name Country Age"
> let rows = map words ["1 USA 20", "2 UK 19"]
> h
["Name","Country","Age"]
> rows
[["1","USA","20"],["2","UK","19"]]
> putStr $ formatTable h rows
|Name|Country|Age|
+----+-------+---+
|1   |USA    |20 |
|2   |UK     |19 |

我现在就试过了,当我在输入(意外的反斜杠(lambda))printfieldName::[String]->String printfieldName[]=[]concatMap(\x->“|”):x++“\t”)concatMap(printf”|%s\t”)中执行语法错误时,我得到了一个错误thanks@dlna需要更多信息Prelude Text.Printf>putStrLn$concatMap(Printf“|%s\t”)(map(:[])“hello”)| h | e | l | l | oi现在尝试了这个,我在输入中执行语法错误(意外的反斜杠(lambda))printfieldName::[String]->String printfieldName[]=[]concatMap(\x->'|':x++“\t”)concatMap(printf”|%s\t“)thanks@dlna需要更多信息Prelude Text.Printf>putStrLn$concatMap(Printf“|%s\t”)(map(:[])“您好”)| h | e | l | l |我是haskell的新手,请您解释一下您编写的函数的流程。它起作用了!:)谢谢你!(++)“|”创建一个函数,该函数在每个单词前面加上“|”前缀,因此(+++“|”test“->”| test“然后,
map”将此函数应用于将其转换为[“|1”、“|USA”、“|20”…]的单词列表,然后
unwords'将它们连接成一个单词之间有空格的字符串。添加final|时需要“++”|“”;抱歉,注释中的所有换行都已丢失。我是新来的:)是的,评论真的没有格式可言。一般来说,通过编辑帖子来添加解释会更好。我是haskell的新手,你能解释一下你写的函数的流程吗。它起作用了!:)谢谢你!(++)“|”创建一个函数,该函数在每个单词前面加上“|”前缀,因此(+++“|”test“->”| test“然后,
map”将此函数应用于将其转换为[“|1”、“|USA”、“|20”…]的单词列表,然后
unwords'将它们连接成一个单词之间有空格的字符串。添加final|时需要“++”|“”;抱歉,注释中的所有换行都已丢失。我是新来的:)是的,评论真的没有格式可言。一般来说,最好通过编辑文章来添加解释。