
Firstly, because "resBDwords :: Maybe String", not "resBDwords :: String",
"lgBDwords = length resBDwords" probably is not what you want -- it does
not give you the number of words in the String that may be in there.
Second, for the problem you asked about, you could just use a function that
takes a String and do it "the hard way" like you said, using case outside
before calling the function. Another way is to use an applicative functor
to allow you to have a "Maybe String -> Maybe String -> Maybe String". This
is used once for each "++" that you want to do.
I don't know exactly what you need to accomplish but I would just write a
function "f :: String -> Maybe String" implementing the logic you listed in
the second code snippet but operating on String instead of "Maybe String"
and do "join . fmap f $ resBDwords".
On Tue, Jan 8, 2019 at 12:13 AM Damien Mattei
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.... :-)
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.