[Haskell-cafe] concatenate two Maybe String...
Damien Mattei
mattei at oca.eu
Mon Jan 7 16:13:13 UTC 2019
hello,
i have a variable resBDwords of type ( i expect) Maybe [String], for
info it is integer and fractional part of a number
example looks like this :
resBDwords =Just ["-04","3982"]
i want to concatanate "-04" and "3982" in the example, i begin to
understand fmap to use the functor hidden in the Maybe ,it worked
previously:
let resBDstr = fmap Tx.unpack resBDtxt
putStr "resBDstr ="
putStrLn (show resBDtxt)
let resBDwords = fmap words resBDstr
putStr "resBDwords ="
putStrLn (show resBDwords)
which gives:
resBDtxt ="-04 3982"
resBDstr =Just "-04 3982"
just after in my code i have this to concatanate the two strings f and s
that are the first and second element of the array:
putStr "resBDwords ="
putStrLn (show resBDwords)
let lgBDwords = length resBDwords
let resBDstrFloat = if lgBDwords == 0
then trace "WARNING: BD contains no words"
Nothing
else
if lgBDwords == 1
then trace "WARNING: BD contains only
one word" fmap head resBDwords
else let f = fmap head resBDwords
s = fmap (head . tail) resBDwords
in f ++ "." ++ S
but i do not know how to concatanate the Maybe String in an elegant way,
using somethin like fmap variable which have handled Nothing (from
Maybe) automatically i need the counter part for multipe variable
i do not want to do it using the hard way with case... of Just x ->
nothing .........
i got this error :
*Main> :load UpdateSidonie
[1 of 1] Compiling Main ( UpdateSidonie.hs, interpreted )
UpdateSidonie.hs:339:43: error:
• Couldn't match expected type ‘[Char]’
with actual type ‘Maybe String’
• In the first argument of ‘(++)’, namely ‘f’
In the expression: f ++ "." ++ s
In the expression:
let
f = fmap head resBDwords
s = fmap (head . tail) resBDwords
in f ++ "." ++ s
|
339 | in f ++ "." ++ s
| ^
UpdateSidonie.hs:339:43: error:
• Couldn't match expected type ‘Maybe String’
with actual type ‘[Char]’
• In the expression: f ++ "." ++ s
In the expression:
let
f = fmap head resBDwords
s = fmap (head . tail) resBDwords
in f ++ "." ++ s
In the expression:
if lgBDwords == 1 then
trace "WARNING: BD contains only one word" fmap head resBDwords
else
let
f = fmap head resBDwords
s = fmap (head . tail) resBDwords
in f ++ "." ++ s
|
339 | in f ++ "." ++ s
| ^^^^^^^^^^^^^
UpdateSidonie.hs:339:55: error:
• Couldn't match expected type ‘[Char]’
with actual type ‘Maybe String’
• In the second argument of ‘(++)’, namely ‘s’
In the second argument of ‘(++)’, namely ‘"." ++ s’
In the expression: f ++ "." ++ s
|
339 | in f ++ "." ++ s
| ^
Failed, no modules loaded.
for now this page has been of valuable help:
https://pbrisbin.com/posts/maybe_is_just_awesome/
i'm sure it's an obvious question but.... :-)
More information about the Haskell-Cafe
mailing list