Haskell working with Maybe lists - list

I'm pretty new to haskell and don't get how to work with Maybe [a]. Normally I'm coding 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 IDs and find out only the IDs that match in both files. Reading the files is not a big thing, it works wonderfully easy. Now, I get two lists of Maybe[Ids] (for a simple example, just think the IDs are Int). So the function I need looks like this
playWithMaybe :: (Maybe [a]) -> (Maybe [Int]) -> [Int]
Now I want to access list members as I used to like this
playWithMaybe (x:xs) (y:ys) = undefined
But unfortunately it's not allowed, GHC says for both lists
Couldn't match expected type ‘Maybe [Int]’ with actual type ‘[t0]’
So I played around a little bit but didn't find a way to access list members. Can someone help me out? A little bit of explanation would be a great thing!

To approach your problem from a different direction, I would argue you don't want a function that processes two Maybe [a]. Bear with me:
Fundamentally, the operation you want to do operates on two lists to give you a new list, eg,
yourFunction :: [a] -> [a] -> [a]
yourFunction a b = ...
That's fine, and you can and should write yourFunction as such. The fact that the data you have is Maybe [a] captures some additional, auxiliary information: the operation that created your input lists may have failed. The next step is to chain together yourFunction with the auxiliary information. This is exactly the purpose of do notation, to mix pure operations (like yourFunction) with contexts (the fact that the creation of one of your input lists may have failed):
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
But then it turns out there are other kinds of contexts you might want to work with (a simple one, instead of Maybe that just captures "something bad happened", we can use Either to capture "something bad happened, and here is a description of what happened). Looking back at playWithMaybe, the "Maybe-ness" only shows up in one place, the Just in the last line. It turns out Haskell offers a generic function pure to wrap a pure value, like what we get from yourFunction, in a minimal context:
playWithMaybe' :: Maybe [a] -> Maybe [a] -> Maybe [a]
playWithMaybe' maybeA maybeB =
do a <- maybeA
b <- maybeB
pure (yourFunction a b)
But then Haskell also has a generic type to abstract the idea of a context, the Monad. This lets us make our function even more generic:
playWithMonad :: Monad m => m [a] -> m [a] -> m [a]
playWithMonad mA mB =
do a <- mA
b <- mB
pure (yourFunction a b)
Now we have something very generic, and it turns out it is so generic, it's already in the standard library! (This is getting quite subtle, so don't worry if it doesn't all make sense yet.)
import Control.Applicative
play :: Monad m => m [a] -> m [a] -> m [a]
play mA mB = liftA2 yourFunction mA mB
or even
import Control.Applicative
play' :: Monad m => m [a] -> m [a] -> m [a]
play' = liftA2 yourFunction
Why did I switch from Monad to Applicative suddenly? Applicative is similar to Monad, but even more generic, so given the choice, it is generally better to use Applicative if you can (similar to my choice to use pure instead of return earlier). For a more complete explanation, I strongly recommend Learn You a Haskell (http://learnyouahaskell.com/chapters), in particular chapters 11 and 12. Note- definitely read chapter 11 first! Monads only makes sense after you have a grasp on Functor and Applicative.

In general:
yourFunction Nothing Nothing = ...
yourFunction (Just xs) Nothing =
case xs of
[] -> ...
x':xs' -> ...
-- or separately:
yourFunction (Just []) Nothing = ...
yourFunction (Just (x:xs)) Nothing = ...
et cetera. Which cases need to be treated separately depends on the specific function. More likely you would combine functions working on Maybe with functions working on [].
If you want to "Just return an list with no elements" for Nothing, then you can write
maybeToList1 :: Maybe [a] -> [a]
maybeToList1 Nothing = []
maybeToList1 (Just xs) = xs
A better way to write the same function is maybeToList1 = maybe [] id (docs for maybe) or maybeToList1 = fromMaybe [], but since you are just starting you may want to come back to this one later.

Like others have said, [Int] and Maybe [Int] are not the same thing. Maybe [Int] includes the extra information that the list may or may not be there. You said that you read the Ids from files. Perhaps, the Maybe signifies whether the file existed or not, while the empty list signifies that the file did exist but contained no Ids.
If you want to work with the list, you first need to define what to do if there is no list. You can use this function to extract the list:
fromMaybe :: a -> Maybe a -> a
Maybe you want to treat having no list the same as having an empty list:
fromMaybe [] :: Maybe [a] -> [a]
Maybe you want to crash the entire program:
fromMaybe (error "Missing list") :: Maybe a -> a
There is also the more general maybe function, which you can use if the default value is not of the same type as what's contained in the Maybe:
maybe :: b -> (a -> b) -> Maybe a -> b
Both these functions are defined in the module Data.Maybe.
You can also just work with the lists as if they existed and worry about their existence later by using Applicative. You said you wanted to find the Ids common to both lists. You can do it like this:
maybeCommonIds :: Maybe [Int] -> Maybe [Int] -> Maybe [Int]
maybeCommonIds xs ys = intersect <$> xs <*> ys
intersect is defined in Data.List. Using maybeCommonIds will result in Maybe [Int]. The list contained inside will hold the common ids, but if either of the two lists did not exist, there is not list of common ids. In your case, this might be the same as having no common ids. In that case, you might want to pass the result to fromMaybe [], to get back the list.

Related

Haskell I/O: Filtering empty lists in CSV data-set

Once again I'm stuck with haskell. This time I'm supposed to filter a CSV-dataset, using IO which I'm struggling to understand. Any additional related information is appreciated.
The structure is predetermined, and to me it seems messy. Is this really how one should do it? (I still have to do it this way, so this is a little "on the side"-question).
getRawData' :: String -> IO [[String]]
getRawData' fn = do
s <- readFile fn
return $ parseCSVsimple s
getRawData :: String -> IO [[String]]
getRawData fn = do
d <- getRawData' fn
return (dropEmpty d)
dropEmpty [[String]] -> [[String]]
DropEmpty - This is where I need help For a "normal" list I would just filter it straightforward, but this IO and different functions calling each other has me confused.
Sorry for stupid questions, I've programmed for 8 months and had to use 6-7 languages since then. Any general information related to the question is very much appreciated as well.
EDIT; FOLLOW-UP:
Thanks to good answers I have improved my understanding and implemented the following code:
dropEmpty :: [[String]] -> [[String]]
dropEmpty = filter(not . null)
However, I still see empty lists in the list. Is this because I am checking if the whole list of lists is empty? How do I sepcify that I want to check the "internal" lists?
The do keyword is just a sugar syntax for monads
Monad are defined like this:
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
Your getRawData function is equivalent to
getRawData fn = getRawData' fn >>= \d -> return (dropEmpty d)
As you can see in the monad definition, the second argument of >>= is the inner type of the monad, in your case, it's [[String]]
So there is nothing wrong with your code, you can use your dropEmpty without worrying about IO
getRawData :: String -> IO [[String]]
getRawData fn = do
d <- getRawData' fn
return (dropEmpty d)
getRawData and return are the monadic actions in this do block. All other expressions do not have the IO type.
In particular, the expression to the left of <-, namely d, has just the plain type [[String]]. All parameters, namely fn :: String and (dropEmpty d) :: [[String]], are also not in IO.
So you can get non-monadic values (e.g. d) out of the monadic actions (e.g. getRawData') easily. However, you can only do so temporarily: You cannot make d escape the do block, only IO d. This is a "core feature" of monads in general.
This is only a very superficial explanation to give you a quick start on monadic code. I recommend you read up on monads. Learn how monads are desugared. Take the state monad for example, and understand what problem it solves by trying to re-implement it.

takeWhile, lambdas, and pattern matching

I'm pretty new to Haskell and still trying to learn the ropes. My problem is this:
I am trying to take all of the numbers which when added to the number after it in a list is less than a certain number. In a more formal sense, how can I use sublists of my list as arguments for my filtering boolean function?
For example: we have the list [a0,a1,a2,...], how can I take from the list while a_n + a_(n+1) < c (where c is just some number)?
Ultimately I am going to be using this to create a sequence of root approximations of a function. Currently I have an infinite list of approximations (since I don't know where to stop yet) and I would like to take all of the approximations up until an approximation that has some tolerable error. The error of the approximation p_n is given as a function of p_n, p_(n-1), p_(n-2). Lets call this function f. So ideally I would like to keep taking elements from the list while
f(p_(n),p_(n-1),p_(n-2)) > error.
The signature of the function you want to write looks like this
takeLessThan :: (Num a, Ord a) => a -> [a] -> [a]
takeLessThan bound xs = ...
You can extract each pair of the list by zipping the list with the tail of itself.
pairs :: [b] -> [(b, b)]
pairs xs = zip xs (tail xs)
So the pairs of the list [1,2,3,4,5] will give you a tuple of (an, an+1).
[(1,2),(2,3),(3,4),(4,5)]
From there you can use the filter function from the Prelude to select the elements you want based on the function (a -> Bool).
filter :: (a -> Bool) -> [a] -> [a]
I think its easier to break down your problem into multiple steps intead of trying to solve it all at once with a super smart filtering function.
Start with a list [a0 ... an]
Turn it into a list of pairs [ (a0, a1) ... (a_n-1, an) ] with zip
Filter the interesting pairs with takeWhile (or filter, depending on what you want to do).
Turn the list of good pairs back into a list with just the first numbers using map
Don't worry about performance or about generating wasteful intermediate lists. Everything should be efficiently created on demand due to the lazy evaluation.

Why is Haskell giving "ambiguous type variable" error?

A past paper problem asked me; to define a function p :: [a] -> [a] that swaps every two items in a list. Your function should swap the first with the second item, the third
with the fourth, and so on. define one by list comprehension another by recursion.
Well this is what I came up with:
import Test.QuickCheck
p :: [a] -> [a]
p [] = []
p xs = concat [ [y,x] | ((x,y),i) <- zip (zip xs (tail xs)) [1..], odd i]
q :: [a] -> [a]
q [] = []
q (x:y:zs) | even (length zs) = [y,x] ++ q zs
| otherwise = error "The list provided is not of even length"
prop_2 xs = even (length xs) ==> p xs == q xs
check2 = quickCheck prop_2
The functions work fine, but I wanted to check if the two are identical, so I put the quickCheck below; but this gives me an error for some reason saying
"ambiguous type variable [a0] arising from the use of prop_2"
I just don't understand what's wrong here, I looks perfectly sensible to me...
what exactly is Haskell complaining??
Let's start by commenting out check2 and asking GHCi what the type of prop_2 is:
*Main> :t prop_2
prop_2 :: Eq a => [a] -> Property
Ok, so the way you've written prop_2, it works for lists of any element type that is in the equality class.
Now, you want to pass prop_2 to the quickCheck function. Let's look at the type of quickCheck next:
*Main> :t quickCheck
quickCheck :: Testable prop => prop -> IO ()
This function actually has an immensely general type. It works on anything that's in the Testable class. So how does this Testable class work? There are a couple of base instances here, for example:
instance Testable Bool
instance Testable Property -- this is a simplification, but it's more or less true
These instances are defined in the QuickCheck library and tell you that you can quick-check
constant Booleans as well as elements of type Property.
Now, testing properties that do not depend on any inputs isn't particularly interesting. The interesting instance is this one:
instance (Arbitrary a, Show a, Testable prop) => Testable (a -> prop)
What this says is that if you know how to generate random values of a particular type (Arbitrary a) and how to show values of that type (Show a), then you can also test functions from that type a to an already testable type prop.
Why? Because that's how QuickCheck operates. In such a situation, QuickCheck will consult the Arbitrary instance in order to come up with random test cases of type a, apply the function to each of them, and check if the outcome is positive. If any of the tests fails, it will print a message informing you about the test failure, and it will print the test case (that's why there's a Show requirement, too).
Now, in our situation this means that we should be able to quick-check prop_2: it is a function that results in a Property. The important thing is that the function argument (of type [a] as long as Eq a holds) is a member of class Arbitrary and a member of class Show.
Here we arrive at the source of the error. The information available is not sufficient to make that conclusion. As I said in the beginning, prop_2 works for lists of any element type that admits equality. There's no built-in rule that says that all these type are in Arbitrary and Show. But even if there was, what kind of lists should QuickCheck generate? Should it generate lists of Booleans, lists of unit type, lists of functions, lists of characters, lists of integers? So many options, and the choice of element type may well affect whether you find a bug or not. (Consider that GHC would pick the unit type () with just one element. Then your property would hold for any two functions p and q that preserve the input lists' length, regardless of whether they have your desired swapping property or not.)
This is why you need to provide extra type information to GHC so that it can resolve which element type to use for the list. Doing so is simple enough. You can either annotate prop_2 itself:
prop_2 :: [Integer] -> Property
Or, if you don't want that (because you might want to run the tests on different kinds of lists without reimplementing prop_2), you can add a type annotation when calling quickCheck:
check2 = quickCheck (prop_2 :: [Integer] -> Property)
Now the code compiles, and we can run check2:
Main*> check2
+++ OK, passed 100 tests.

Is there a way for easy construction of a list of repeated elements in Haskell without helper functions?

Given a tuple of type (Int, a) such as (n,c), I wish to construct a list [a] where the element c is repeated n times, i.e., (4, 'b') becomes "bbbb". My current solution is the following:
decode :: (Int, a) -> [a]
decode (n, a) = map (\x -> a) [1..n]
As you can see, I'm mapping an anonymous function that always returns a over a list of n elements, the first n positive integers. Is there a more efficient way to do this? I feel bad about constructing a list of integers and never using it. Another solution would use a helper function and recurse down n, but that seems messy and overcomplicated. Is there perhaps something akin to the following python code?
'b'*4
uncurry replicate
Prelude> :t uncurry replicate
uncurry replicate :: (Int, b) -> [b]
Prelude> uncurry replicate (4, 'b')
"bbbb"
There is a builtin replicate for that.
Check out Hoogle for when you need to find if there is already a function that does what you want somewhere.
You want replicate.
A good way to find those things:
http://haskell.org/hoogle/?hoogle=Int+-%3E+a+-%3E+%5Ba%5D

`Ord a =>` or `Num a =>`

I have the following functions:
which (x:xs) = worker x xs
worker x [] = x
worker x (y:ys)
| x > y = worker y ys
| otherwise = worker x ys
and am wondering how I should define the types signatures of these above functions which and worker?
For Example, which of the following ways would be best as a type signature for worker?
worker :: Num a => a -> [a] -> a,
or
worker :: Ord a => a -> [a] -> a?
I'm just really confused and don't get which these three I should choose. I'd appreciate your thoughts. Thanks.
If you define the function without an explicit type signature, Haskell will infer the most general one. If you’re unsure, this is the easiest way to figure out how your definition will be read; you can then copy it into your source code. A common mistake is incorrectly typing a function and then getting a confusing type error somewhere else.
Anyway, you can get info on the Num class by typing :i Num into ghci, or by reading the documentation. The Num class gives you +, *, -, negate, abs, signum, fromInteger, as well as every function of Eq and Show. Notice that < and > aren’t there! Requiring values of Num and attempting to compare them will in fact produce a type error — not every kind of number can be compared.
So it should be Ord a => ..., as Num a => ... would produce a type error if you tried it.
If you think about what your functions do, you'll see that which xs returns the minimum value in xs. What can have a minimum value? A list of something Orderable!
Ask ghci and see what it says. I just copy-pasted your code as is into a file and loaded it into ghci. Then I used :t which is a special ghci command to determine the type of something.
ghci> :t which
which :: (Ord t) => [t] -> t
ghci> :t worker
worker :: (Ord a) => a -> [a] -> a
Haskell's type inference is pretty smart in most cases; learn to trust it. Other answers sufficiently cover why Ord should be used in this case; I just wanted to make sure ghci was clearly mentioned as a technique for determining the type of something.
I would always go with the Ord type constraint. It is the most general, so it can be reused more often.
There is no advantage to using Num over Ord.
Int may have a small advantage as it is not polymorphic and would not require a dictionary lookup. I would stil use Ord and use the specialize pragma if I needed to for performance.
Edit: Altered my answer after comments.
It depends on what you want to being able to compare. If you want to being able to compare Double, Float, Int, Integer and Char then use Ord. If you only want to being able to compare Int then just use Int.
If you have another problem like this, just look at the instances of the type class to tell which types you want to be able to use in the function.
Ord documentation