English 中文(简体)
New Line Haskell
原标题:New Line Haskell

Hey。 关于本周的辅导,其中一个问题要求采用其他功能格式Line和格式List编制一个行文清单,从而形成一种功能格式。

我的法典就是这样做的。

type Line = String

formatLine :: Line -> String
formatLine l = l ++ "
"

formatList :: (a -> String) -> [a] -> String
formatList f [] = []
formatList f xs = f (head xs) ++ formatList f (tail xs)

formatLines :: [Line] -> String
formatLines xs = formatList formatLine xs

The code seems (to me, at least) like it should work, but instead of creating a new line where " " is, gets appended to the string.

任何帮助都将受到高度赞赏。

最佳回答

这是因为你可能使用印刷结果。 相反,使用putStr。 观察:

Prelude> print "test
test
"
"test
test"
Prelude> putStr "test
test
"
test
test

除此以外,您可使用对应方式在以下网址上填写:formatList,但>>>>>>>> >>>>>>>>>>>和

formatList :: (a -> String) -> [a] -> String
formatList f [] = []
formatList f (x:xs) = f x ++ formatList f xs

但是,实际上没有必要界定<代码>formatList yourself,因为它与

formatList :: (a -> String) -> [a] -> String
formatList = concatMap

Combining all this, you can also just write (note that (++ " ") is a section):

formatLines :: [String] -> String
formatLines = concatMap (++ "
")

http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Aunlines”rel=“noreferer”>unlines:

formatLines :: [String] -> String
formatLines = unlines
问题回答

公正的审判

formatLines = unwords




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签