简体   繁体   English

试图在haskell中获得2个字符串的首字母

[英]Trying to get the initials of 2 string in haskell

initt [f:_] [l:_] = f ++ " " ++ l

And then i call 然后我打电话

initt "First" "Last"

I get an error, saying Couldn't match type 'Char' with '[Char]' 我收到一个错误,说无法将'Char'与'[Char]'匹配

I really don't get it. 我真的不明白。

I think you want 我想你想要的

initt (f:_) (l:_) = [f, ' ', l]

[f:_] is equivalent to [(f:_)] which would match something like ["First"] [f:_]相当于[(f:_)] ,它匹配像["First"]这样的东西

Also (++) works on strings, while f & l are supposed to be chars. 此外(++)适用于字符串,而fl应该是字符。 At best, you could do something like [c] ++ " " ++ [l] but [f, ' ', 'l'] is much better & simpler. 充其量,你可以做类似[c] ++ " " ++ [l]事情,但[f, ' ', 'l']更好更简单。

++ is list concatenation. ++列表连接。 Either pack f and l into lists: fl打包到列表中:

[f] ++ " " ++ [l]

Or create it like Ingo suggested: 或者像Ingo建议的那样创建它:

[f, ' ', l]

Also, pattern matching on head:tail implies it's a list, so instead of [f:_] , you need simply (f:_) . 此外, head:tail上的模式匹配意味着它是一个列表,所以不需要[f:_] ,而只需要(f:_)

Which brings us to the whole solution: 这将我们带到整个解决方案:

initt (f:_) (l:_) = [f, ' ', l]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM