Reputation: 320
I am trying to create a recursive function that takes and input List of List that looks like this: [[1,2], [4], [3], [1]]
and returns a Bool. It is supposed to check whether all lists include at least one unique number. I am trying to do this recursively using the two function below.
Function that removes all elements from the first list in the second list:
helper :: Eq a => [a] -> [a] -> [a]
helper a b = filter (`notElem` b) a
Main function:
function :: [[Int]] -> Bool
function [] = True
function (x:y:xs) = (not (null r)) and (function (r ++ xs))
where r = helper(x,y)
However, I get these two errors from the compiler:
Couldn't match expected type ‘(t0 Bool -> Bool) -> Bool -> Bool’
with actual type ‘Bool’
The function ‘not’ is applied to three arguments,
but its type ‘Bool -> Bool’ has only one
In the expression: (not (null r)) and (function (r ++ xs))
In an equation for ‘function’:
function (x : y : xs)
= (not (null r)) and (function (r ++ xs))
where
r = helper (x, y)
I am new to Haskell and not fully comfortable with the Haskell syntax.
Upvotes: 0
Views: 135
Reputation: 2549
First issue - calling a function with multiple arguments. This is done with the following syntax: myFunction a b
(not myFunction (a, b)
).
The reason for this is that (a, b)
is a tuple. Here's a link with some info on tuples
Second issue - you are using and instead of &&
Third issue - you are trying to combine r
and xs
with ++
but the type of r
is [Int]
whereas the type of xs
is [[Int]]
. If your goal is to prepend r
to xs
then you can do this with :
helper :: Eq a => [a] -> [a] -> [a]
helper a b = filter (`notElem` b) a
function :: [[Int]] -> Bool
function [] = True
function (x:y:xs) = not (null r) && function (r : xs)
where r = helper x y
Upvotes: 0