0

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.

3
  • 8
    and doesn't do what you think, you're looking for && Commented Apr 19, 2021 at 9:22
  • a guess: you try to check if the second list contains any element not in the first, the third should contain one element not in the first nor the second, ...? Commented Apr 19, 2021 at 10:03
  • 3
    helper(x,y) is wrong since helper does not take a pair as argument. Use helper x y instead. Commented Apr 19, 2021 at 10:45

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.