[Haskell-cafe] concatenate two Maybe String...
Doaitse Swierstra
doaitse at swierstra.net
Tue Jan 8 13:42:00 UTC 2019
Something like:
resBDstrFloat =
case resDBWords of
Nothing -> trace "WARNING: BD contains no words” Nothing
Just [] -> trace "WARNING: BD contains no words” Nothing
Just [v] -> trace "WARNING: BD contains only one word” (Just v)
Just [v1,v2]) -> Just (v1 ++ “.” ++ v2)
- -> trace “unexpected garbage” Nothing
seems optimal and easiest to understand to me,
Doaitse
> Op 8 jan. 2019, om 11:40 heeft Damien Mattei <damien.mattei at gmail.com> het volgende geschreven:
>
> i had this solution for now:
>
> let resBDstrFloat = if lgBDwords == Just 0
> then trace "WARNING: BD contains no words" Nothing
> else
> if lgBDwords == Just 1
> then trace "WARNING: BD contains only one word" fmap head resBDwords
> else let f = fmap head resBDwords
> s = fmap (head . tail) resBDwords
> mp = Just "." :: Maybe String
> (+++) = liftM2 (++)
> in f +++ mp +++ s
>
> still searching to express it with <*> ..., also there is the problem of if i define "." simply as it is not a Maybe String it fails, perheaps some viadic function that accept multi-type variable but this is complex to do.
>
> On Mon, Jan 7, 2019 at 11:58 PM Jake <jake.waksbaum at gmail.com> wrote:
>
>
> On Mon, Jan 7, 2019 at 5:55 PM Jake <jake.waksbaum at gmail.com> wrote:
> Like Josh mentioned, Applicative Functors are what you want. There are two idiomatic ways to do it:
> • You can just use liftA2, which has type (a -> b -> c) -> f a -> f b -> f c. That means it lifts a binary function to some applicative functor like maybe, so liftA2 (++) :: Maybe String -> Maybe String -> Maybe String
> • In general, for any arity f that you want to lift to an applicative functor. So you have a function g that takes a bunch of arguments of types a, b, c, ... and gives you back an r and you want to get a function that takes an f a, f b, f c, ... and gives you back an f r, you can write f <$> a <*> b <*> c ... This works because <$> let's you apply g to type f a and gives you an f (b -> c ..) -> f r, and <*> basically let's you take the function back out of the f and apply it to the b to get a f (c ..) -> f r and so on. tl;dr you can write (++) <$> s1 <*> s2. In fact, liftA2 must satisfy the equation liftA2 f x y = f <$> x <*> y so these are the same thing.
> בתאריך יום ב׳, 7 בינו׳ 2019, 17:41, מאת ☂Josh Chia (謝任中) <joshchia at gmail.com>:
> 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 <mattei at oca.eu> wrote:
> 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.
> _______________________________________________
> 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.
> _______________________________________________
> 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.
> _______________________________________________
> 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.
More information about the Haskell-Cafe
mailing list