I have a data called “Sample”, defined as
data Sample = Test1 (String,[[String]])
| Test2 (String,[[String]])
Then I create a list[Sample] called “Samples”, say
[Test1 ("Sample1",[["works","running"]]), Test2 ("Sample2", []),
Test1 ("Sample3", [["aborts"]] ]
Now, I need to check whether “aborts” is present under “Sample3”. I have a basic
idea of how to do it but I am not sure how to implement it in Haskell.
My idea is
check:: String -> [String] -> [Sample] -> Bool
check samplename result samples =
I can call it by:
check "Sample3" ["aborts"] Samples
But how do I implement this function.
I have solved like this but I am looking for a better version.
My version is:
check:: String -> [String] -> [Sample] -> Bool
check samplename result samples =
if( "True" `elem` final )
then True
else False
where
final = [x | sample <- samples,
x <- [ case sample of
Test1 (name,res) =
if name == samplename && (result `elem` res)
then "True"
else "False"
Test2 (name, res) = "False"]]
Here is my version of the solution. But I dont think your data represent any real problem , you can easily use Map if you want to store something like [Sample] which is basically a list of key value pairs. Well the idea behind my solution is also inspired from Map . I have written a
lookup'function similar to lookup which given a key returns the value . writing the rest function is trivial then. Similar to your approach but less messy .