简体   繁体   中英

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]'

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"]

Also (++) works on strings, while f & l are supposed to be chars. At best, you could do something like [c] ++ " " ++ [l] but [f, ' ', 'l'] is much better & simpler.

++ is list concatenation. Either pack f and l into lists:

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

Or create it like Ingo suggested:

[f, ' ', l]

Also, pattern matching on head:tail implies it's a list, so instead of [f:_] , you need simply (f:_) .

Which brings us to the whole solution:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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