The way I always recommend newcomers approach this problem is this:
- Look for ways to solve the problem by using standard library functions.
- When you've succeeded at that, write your own version of every library functions you used.
In this case, your description of what you're thinking can be finessed into a #1-style solution. Let's quote it:
So far I'm thinking of taking the first element from list one and comparing it to all the elements in list 2 and do that for all the elements and then return true or false.
Here, instead of thinking of doing that just with the first element, picture doing the same thing to all the elements as one step to the solution. There's a standard function that captures this pattern:
-- The `map` function applies the function given as the first argument
-- to all of the elements of the second argument. The result is a list
-- of all the individual results.
--
-- Example:
--
-- >>> map (+10) [1, 2, 3, 4]
-- [11, 12, 13, 14]
map :: (a -> b) -> [a] -> [b]
So if you can write a function that tests for one value whether it exists in the second list, you can use map to apply that function to all elements of the first list:
step1 :: Eq a => [a] -> [a] -> [Bool]
step1 xs ys = map checkOne xs
where checkOne x = _fillInTheBlank
(The _fillInTheBlank bit is called a "hole"—you're supposed to actually write the correct code in there!)
The second step would be to check the [Bool] to see whether all of the elements are True. You return True if they are, and False if at least one is false. There is a standard function for that as well:
-- Returns `False` if any element of the list is `False`, `True` otherwise.
and :: [Bool] -> Bool
And now:
listCheck :: Eq a => [a] -> [a] -> Bool
listCheck xs ys = and (map checkOne xs)
where checkOne x = _fillInTheBlank
map :: (a -> b) -> [a] -> [b]
-- Write your own version of `map`
and :: [Bool] -> Bool
-- Write your own version of `and`
Note what I've done: I've split the problem into smaller parts, each of which I can solve with functions like map and and that will be useful in many other cases. That's what you should be shooting for.
listCheck = (==)orlistCheck = Data.List.isPrefixOf- Bergi