Reputation: 1941
I'm trying to write an abbreviate function like so:
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-deferred-type-errors #-}
module Acronym (abbreviate) where
import Data.Text (Text)
import qualified Data.Text as T
abbreviate :: Text -> Text
abbreviate xs = T.concat (map T.head (T.splitOn " " xs))
but I am getting this error:
• Couldn't match type ‘Char’ with ‘Text’
Expected type: [Text]
Actual type: [Char]
• In the first argument of ‘T.concat’, namely
‘(map T.head (T.splitOn " " xs))’
In the expression: T.concat (map T.head (T.splitOn " " xs))
In an equation for ‘abbreviate’:
abbreviate xs = T.concat (map T.head (T.splitOn " " xs))
|
10 | abbreviate xs = T.concat (map T.head (T.splitOn " " xs))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Any help on resolving this would be much appreciated.
Upvotes: 0
Views: 57
Reputation: 10695
T.head
has type Text -> Char
, so the result of map T.head (T.splitOn " " xs)
is a value of type [Char]
. T.concat
has type [Text] -> Text
, so they are not compatible. Use T.pack
instead which has the correct type [Char] -> Text
(or String -> Text
which is the same thing).
Upvotes: 4