How to use maybe in a list in Haskell? - haskell

I am using haskell and the following:
convertToBasis :: [String] -> Maybe [Basis]
convertToBasis [] = Nothing
convertToBasis (h:t) = basis : convertToBasis t
where
basis = toCandidateBasis h
Returns a type of [Maybe Basis] I want a type of Maybe [Basis]
toCandidateBasis is shown below:
toCandidateBasis :: String -> Maybe Basis
toCandidateBasis myStr = if not $ has7UniqueLetters myStr [] then
Nothing
else
Just (dedupAndSort myStr)
The function returns nothing if a String does not have 7 unique characters otherwise it removes duplicates and sorts the String alphabetically and return that result. Now the return type of the function is Maybe Basis. Hence when I when I get a list of strings and want to convert them to their respective candidate basises one by one, it will concat all the maybes and return me a type of:
[Maybe Basis]
But I want a type of
Maybe [Basis]
It should return Nothing only when the given Basis array is empty otherwise return some result. How do I do this?

There's really no reason to use a Maybe [a] unless there is a semantic difference between Nothing and Just [], which it sounds like there is not in your case. However, it is a fairly common use case to reduce a list [Maybe a] to a list [a] by removing all the Nothings— so common, in fact, that there's a builtin function for doing that: catMaybes :: [Maybe a] -> [a].

Related

Using Ord to create and compare empty lists

I am trying to create an empty list then confirm that it is empty so I can later on insert elements into it.
I tried to simply assign the function to an empty list but when i try to confirm that it is empty I get an Non-exhaustive patterns in function exception.
Here is what I have so far.
emptyList::Ord a => [(a,b)]
emptyList = []
isEmpty::Ord a => [(a,b)] -> Bool
isEmpty[(a,b)] = null [undefined, undefined]
I am trying to get something like
isEmpty emptyList
True
I guess my question here is that, how can I confirm that the list is empty based on the given type?
I think you are confusing the type ([(a,b)]) with the patterns. In your isEmpty function, you write:
isEmpty::Ord a => [(a,b)] -> Bool
isEmpty [(a,b)] = null [undefined, undefined]
But this does not mean that you are going to match lists of type [(a,b)]. You already said that in your type signature. Your [(a,b)] in the second line, means you define a pattern. The pattern sayd that you are only matching lists that contain exactly one element: a 2-tuple, with a the first item of that tuple, and b the second item of that tuple.
If you then pass it an empty list, or a list with two or more elements, then the pattern will not match, and hence it will raise an error.
If you want to match any list, you can simply use a variable:
isEmpty :: Ord a => [(a,b)] -> Bool
isEmpty ls = null ls
here isEmpty thus will call null with that variable. We can perform an η-reduction here, and write:
isEmpty :: Ord a => [(a,b)] -> Bool
isEmpty = null
There is no need at all, to restrict ourselves to only lists with 2-tuples where the first item of these tuples has a type that is a member of the Ord typeclass however, we can let this work with any list. So we can generalize the type to:
isEmpty :: [a] -> Bool
isEmpty = null
In fact null can operate over any Foldable, since it has type null :: Foldable f => f a -> Bool.
Given the above function defintion, there is no need to implement your own isEmpty, you can simply call null emptyList.

converting a list of string into a list of tuples in Haskell

I have a list of strings:
[" ix = index"," ctr = counter"," tbl = table"]
and I want to create a tuple from it like:
[("ix","index"),("ctr","counter"),("tbl","table")]
I even tried:
genTuple [] = []
genTuples (a:as)= do
i<-splitOn '=' a
genTuples as
return i
Any help would be appriciated
Thank you.
Haskell's type system is really expressive, so I suggest to think about the problem in terms of types. The advantage of this is that you can solve the problem 'top-down' and the whole program can be typechecked as you go, so you can catch all kinds of errors early on. The general approach is to incrementally divide the problem into smaller functions, each of which remaining undefined initially but with some plausible type.
What you want is a function (let's call it convert) which take a list of strings and generates a list of tuples, i.e.
convert :: [String] -> [(String, String)]
convert = undefined
It's clear that each string in the input list will need to be parsed into a 2-tuple of strings. However, it's possible that the parsing can fail - the sheer type String makes no guarantees that your input string is well formed. So your parse function maybe returns a tuple. We get:
parse :: String -> Maybe (String, String)
parse = undefined
We can immediately plug this into our convert function using mapMaybe:
convert :: [String] -> [(String, String)]
convert list = mapMaybe parse list
So far, so good - but parse is literally still undefined. Let's say that it should first verify that the input string is 'valid', and if it is - it splits it. So we'll need
valid :: String -> Bool
valid = undefined
split :: String -> (String, String)
split = undefined
Now we can define parse:
parse :: String -> Maybe (String, String)
parse s | valid s = Just (split s)
| otherwise = Nothing
What makes a string valid? Let's say it has to contain a = sign:
valid :: String -> Bool
valid s = '=' `elem` s
For splitting, we'll take all the characters up to the first = for the first tuple element, and the rest for the second. However, you probably want to trim leading/trailing whitespace as well, so we'll need another function. For now, let's make it a no-op
trim :: String -> String
trim = id
Using this, we can finally define
split :: String -> (String, String)
split s = (trim a, trim (tail b))
where
(a, b) = span (/= '=') s
Note that we can safely call tail here because we know that b is never empty because there's always a separator (that's what valid verified). Type-wise, it would've been nice to express this guarantee using a "non-empty string" but that may be a bit overengineered. :-)
Now, there are a lot of solutions to the problem, this is just one example (and there are ways to shorten the code using eta reduction or existing libraries). The main point I'm trying to get across is that Haskell's type system allows you to approach the problem in a way which is directed by types, which means the compiler helps you fleshing out a solution from the very beginning.
You can do it like this:
import Control.Monda
import Data.List
import Data.List.Split
map ((\[a,b] -> (a,b)) . splitOn "=" . filter (/=' ')) [" ix = index"," ctr = counter"," tbl = table"]

Searching through tuple of more than 2 elements

How can I search a tuple based on the first element or second or third?
I know how to search tuple of two but how do i do it for more than two?
type Stuff = (String, String, Int)
testStuff :: [Stuff]
testStuff = [
("beans","black",5),
("cod","fish",4),
("coke","diet",3)
]
How can I write a function that will search "Stuff" and return the "int" value?
e.g searchStuff "beans" should return 5.
Since you haven't provided your function for searching in a list of pairs, I'm going to assume that you used lookup. Lets focus on find for your new function. find can be found in Data.List (and a more general version in Data.Foldable) and has the following type:
find :: (a -> Bool) -> [a] -> Maybe a
Now, if you need to find something in a list of triples based on the first element, you could use
find (\(a,_,_) -> a == "beans") testStuff
However, this will leave you with a Maybe Stuff. But since Maybe is an instance of Functor, it is easy to change the result to Maybe Int (and is left as an exercise).
The Prelude defines lookup to handle searching a list of pairs. Here's the definition:
-- | 'lookup' #key assocs# looks up a key in an association list.
lookup :: (Eq a) => a -> [(a,b)] -> Maybe b
lookup _key [] = Nothing
lookup key ((x,y):xys)
| key == x = Just y
| otherwise = lookup key xys
Can you see how one would define a similar function to search a list of triples?

Define a haskell function [IO a] -> IO[a]

I am doing a haskell exercise, regarding define a function accumulate :: [IO a] -> IO [a]
which performs a sequence of interactions and accumulates their result in a list.
What makes me confused is how to express a list of IO a ? (action:actions)??
how to write recursive codes using IO??
This is my code, but these exists some problem...
accumulate :: [IO a] -> IO [a]
accumulate (action:actions) = do
value <- action
list <- accumulate (action:actions)
return (convert_to_list value list)
convert_to_list:: Num a =>a -> [a]-> [a]
convert_to_list a [] = a:[]
convert_to_list x xs = x:xs
What you are trying to implement is sequence from Control.Monad.
Just to let you find the answer instead of giving it, try searching for [IO a] -> IO [a] on hoogle (there's a Source link on the right hand side of the page when you've chosen a function).
Try to see in your code what happens when list of actions is empty list and see what does sequence do to take care of that.
There is already such function in Control.Monad and it called sequence (no you shouldn't look at it). You should denote the important decision taken during naming of it. Technically [IO a] says nothing about in which order those Monads should be attached to each other, but name sequence puts a meaning of sequential attaching.
As for the solving you problem. I'd suggest to look more at types and took advice of #sacundim. In GHCi (interpreter from Glasgow Haskell Compiler) there is pretty nice way to check type and thus understand expression (:t (:) will return (:) :: a -> [a] -> [a] which should remind you one of you own function but with less restrictive types).
First of all I'd try to see at what you have showed with more simple example.
data MyWrap a = MyWrap a
accumulate :: [MyWrap a] -> MyWrap [a]
accumulate (action:actions) = MyWrap (convert_to_list value values) where
MyWrap value = action -- use the pattern matching to unwrap value from action
-- other variant is:
-- value = case action of
-- MyWrap x -> x
MyWrap values = accumulate (action:actions)
I've made the same mistake that you did on purpose but with small difference (values is a hint). As you probably already have been told you could try to interpret any of you program by trying to inline appropriate functions definitions. I.e. match definitions on the left side of equality sign (=) and replace it with its right side. In your case you have infinite cycle. Try to solve it on this sample or your and I think you'll understand (btw your problem might be just a typo).
Update: Don't be scary when your program will fall in runtime with message about pattern match. Just think of case when you call your function as accumulate []
Possibly you looking for sequence function that maps [m a] -> m [a]?
So the short version of the answer to your question is, there's (almost) nothing wrong with your code.
First of all, it typechecks:
Prelude> let accumulate (action:actions) = do { value <- action ;
list <- accumulate (action:actions) ; return (value:list) }
Prelude> :t accumulate
accumulate :: (Monad m) => [m t] -> m [t]
Why did I use return (value:list) there? Look at your second function, it's just (:). Calling g
g a [] = a:[]
g a xs = a:xs
is the same as calling (:) with the same arguments. This is what's known as "eta reduction": (\x-> g x) === g (read === as "is equivalent").
So now just one problem remains with your code. You've already taken a value value <- action out of the action, so why do you reuse that action in list <- accumulate (action:actions)? Do you really have to? Right now you have, e.g.,
accumulate [a,b,c] ===
do { v1<-a; ls<-accumulate [a,b,c]; return (v1:ls) } ===
do { v1<-a; v2<-a; ls<-accumulate [a,b,c]; return (v1:v2:ls) } ===
do { v1<-a; v2<-a; v3<-a; ls<-accumulate [a,b,c]; return (v1:v2:v3:ls) } ===
.....
One simple fix and you're there.

what is [a] as a type in Haskell?

This question is about type in Haskell programming language.
When I type
:t []
I get the return as:
[] :: [a]
What is [a]?
Rewriting it like so might make things clearer:
[] :: forall a. [a]
So, since it doesn't contain any value, haskell can't decide on a type and leaves it open. If you use it in a typed expression, however, like
x = []
f :: [Int] -> [Int]
f l = l
y = f x
it automatically gets resolved to [Int], since it is used as this type.
It's a placeholder. It basically means any type could go there. In other words, if you had a function
someFunc :: [Int] -> ()
You could pass [] to it because [] is [a], which will match [Int] (by replacing the a placeholder with the concrete Int type).
Now if you look at, say, id, you'll find it has the type a -> a. This means that it's a function that takes an object of any type, and returns an object of the same type. Interestingly, because the function knows absolutely nothing about the object it's given (since it has no constraints on the type), you know that this function has to do either of two things:
Return the input object, or
Return ⊥ (bottom).

Resources