简体   繁体   中英

Data.Text operations issuing error due to type incompatibility on Haskell

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.

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).

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