Haskell Works with Maybe Lists

I am new to haskell and don't understand how to work with Maybe [a]. Usually I encode OOP (VB.NET), and in my free time I want to learn haskell (don't ask why;)).

So what do I want to do? I want to read two files with numerical identifiers and find out only those identifiers that match in both files. Reading files is not a big thing; it works very easily. Now I get two lists Maybe[Ids](for a simple example, just think that ID is Int). Therefore, the function I need is as follows:

playWithMaybe :: (Maybe [a]) -> (Maybe [Int]) -> [Int]

Now I want to access the list items as I liked it

playWithMaybe (x:xs) (y:ys) = undefined

But unfortunately this is not allowed, the GHC says for both lists

Couldn't match expected type ‘Maybe [Int]’ with actual type ‘[t0]’

So, I played a little, but did not find a way to access the members of the list. Can anybody help me? A little explanation would be great!

+4
source share
3 answers

To approach your problem from a different direction, I would say that you do not want the function to handle two Maybe [a]. Bear with me:

Basically, the operation you want to do works in two lists to give you a new list, for example

yourFunction :: [a] -> [a] -> [a]
yourFunction a b = ...

, yourFunction . , Maybe [a], : , , . - yourFunction . do, (, yourFunction) ( , ):

playWithMaybe :: Maybe [a] -> Maybe [a] -> Maybe [a]
playWithMaybe maybeA maybeB =
  do a <- maybeA   -- if A is something, get it; otherwise, pass through Nothing
     b <- maybeB   -- if B is something, get it; otherwise, pass through Nothing
     Just (yourFunction a b)  -- both inputs succeeded!  do the operation, and return the result

, , , , , (, Maybe, "- ", Either "- " , , ). playWithMaybe, "Maybe -ness" , Just . , Haskell function pure, , , yourFunction, :

playWithMaybe' :: Maybe [a] -> Maybe [a] -> Maybe [a]
playWithMaybe' maybeA maybeB =
  do a <- maybeA
     b <- maybeB
     pure (yourFunction a b)

Haskell , , . :

playWithMonad :: Monad m => m [a] -> m [a] -> m [a]
playWithMonad mA mB =
  do a <- mA
     b <- mB
     pure (yourFunction a b)

- , , , ! ( , , .)

import Control.Applicative
play :: Monad m => m [a] -> m [a] -> m [a]
play mA mB = liftA2 yourFunction mA mB

import Control.Applicative
play' :: Monad m => m [a] -> m [a] -> m [a]
play' = liftA2 yourFunction

Monad Applicative? Monad, , , , , ( pure return ). Learn You a Haskell (http://learnyouahaskell.com/chapters), 11 12. : 11 ! , , Functor Applicative.

+5

:

yourFunction Nothing Nothing = ...
yourFunction (Just xs) Nothing = 
  case xs of
    [] -> ...
    x':xs' -> ...
-- or separately: 
yourFunction (Just []) Nothing = ... 
yourFunction (Just (x:xs)) Nothing = ...

et cetera. , . , Maybe, , [].

" " Nothing,

maybeToList1 :: Maybe [a] -> [a]
maybeToList1 Nothing = []
maybeToList1 (Just xs) = xs

- maybeToList1 = maybe [] id (docs Maybe) maybeToList1 = fromMaybe [], , .

+5

, [Int] Maybe [Int] - . Maybe [Int] , . , . , Maybe , , , , .

, , , . :

fromMaybe :: a -> Maybe a -> a

, , , :

fromMaybe [] :: Maybe [a] -> [a]

, :

fromMaybe (error "Missing list") :: Maybe a -> a

Maybe, , , Maybe:

maybe :: b -> (a -> b) -> Maybe a -> b

Data.Maybe.

, , , Applicative. , , . :

maybeCommonIds :: Maybe [Int] -> Maybe [Int] -> Maybe [Int]
maybeCommonIds xs ys = intersect <$> xs <*> ys

intersectdefined in Data.List. Use maybeCommonIdswill result in Maybe [Int]. The list contained inside will contain common identifiers, but if neither of the two lists exists, the identifier list is missing. In your case, it could be the same as not having common identifiers. In this case, you can pass the result to fromMaybe []to return the list.

0
source

All Articles