type systems - Haskell converting [Char] to Char -
i'm working on haskell program uses whte network.wai
, network.socket.internal
modules. in program, have function, defined so:
prepareip :: request -> [([char], [char])] prepareip req = [("remote", head $ spliton ":" $ show $ remotehost req)]
originally, made monadic expression (mostly readability , clarity), written:
prepareip :: request -> [([char], [char])] prepareip req = ip <- head $ spliton ":" $ show $ remotehost req [("remote", ip)]
while former implementation work properly, monadic 1 threw error can't wrap head around: output type not [([char], [char])]
, [([char], char)]
-- value ip
was, reason, being turned single character rather string!
i tried prototyping in ghci , type-checking using module references :t
in ghci, no avail, , still don't know caused error.
can clue me in on why type conflict happening?
the reason why you're seeing error because you're trying shove monad when doesn't need be. illustrate, adding explicit type signatures everywhere , swapping []
m
:
gethostip :: request -> m [] gethostip req = head $ spliton ":" $ remotehost req prepareip :: request -> m ([char], [char]) prepareip req = ip <- (gethostip req :: m char) [("remote" :: [char], ip :: [char])]
whenever see <-
, right side has have type m a
m
monadic type , a
whatever result of monadic action is. then, left side of <-
has type a
. here you've used <-
try value out of [char]
, when want value of [char]
completely. let
appropriate here, , do
notation superfluous. since you're returning single value, i'd recommend returning (string, string)
instead of [(string, string)]
:
prepareip :: request -> (string, string) prepareip req = let ip = gethostip req in ("remote", ip)
Comments
Post a Comment