[英]Haskell : Transform String -> [List]
I am trying to read n on the first line then n lines of input and print the sum of the first 2 elements from each line such as : 我正在尝试在第一行中读取n,然后读取n行输入,并从每行打印前2个元素的总和,例如:
Input: 输入:
2
1 2
3 4
Output: 输出:
3
7
so far my code looks like: 到目前为止,我的代码如下所示:
import Control.Monad
fromDigits = foldl addDigit 0
where addDigit num d = 10*num + d
first (x:xs) = fromDigits x
second (x:xs) = fromDigits xs
main = interact processInput
processInput input = unlines [perLine line | line <- lines input]
perLine line = first line + second line
but I get the following error 但我收到以下错误
Couldn't match type '[Char]' with 'Char'
无法将类型'[Char]'与'Char'匹配
Couldn't match type 'Char' with '[String]'
无法将类型'Char'与'[String]'相匹配
I am new to Haskell so I am unsure how to solve it. 我是Haskell的新手,所以不确定如何解决它。
Some hints, in order: 一些提示,顺序为:
Char
to Int
or the like. Char
转换为Int
等。
ord
. ord
。 x:xs
pattern, xs
is the rest of the list, not the next element. x:xs
模式中, xs
是列表的其余部分,而不是下一个元素。
[Char]
vs. Char
problem comes from. [Char]
相对于Char
问题的来源。 words
function. words
功能。 show
. show
。 In general, I recommend starting up ghci
and playing with it, just to gain some basic familiarity with Haskell. 通常,我建议启动
ghci
并使用它,以使您对Haskell有一些基本的了解。 Pull up Hoogle or some other Haskell reference in another window... 在另一个窗口中拉起Hoogle或其他Haskell参考...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.