concatenate two Maybe String...

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

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.

On Mon, Jan 07, 2019 at 05:13:13PM +0100, Damien Mattei wrote:
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: ... i do not want to do it using the hard way with case... of Just x -> nothing .........
Just do it the hard way and get some code that works. You can always "improve" it later if you like. "Hard" code that works is orders of magnitude more valuable that "easy" code that doesn't.

from the previous solution and posts i can deduce this solution to concatanate N Maybe String, the function could be written: (+++) = \x y -> (++) <$> x <*> y this is basically equivalent to the previous one: (+++) = liftM2 (++) on f +++ mp +++ s i get results like: resBDstrFloat =Just "-04.3982" i will stay with those solutions. Le 08/01/2019 08:47, Tom Ellis a écrit :
On Mon, Jan 07, 2019 at 05:13:13PM +0100, Damien Mattei wrote:
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: ... i do not want to do it using the hard way with case... of Just x -> nothing .........
Just do it the hard way and get some code that works. You can always "improve" it later if you like. "Hard" code that works is orders of magnitude more valuable that "easy" code that doesn't. _______________________________________________ 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.
-- Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS
participants (3)
-
Damien Mattei
-
Tom Ellis
-
☂Josh Chia (謝任中)