0

I'm new to Haskell and doing some simple exercises. For this exercise I'm writing a function that checks whether one list is a subset of another list using recursion.

Here's the code:

subset [] xs = True

subset (x:xs) ys = if elem x ys == False then False
                   else subset (tail xs) (delete x ys)

I'm getting the following error message:

C:\Functioneel programmeren\week4.hs:9:43: error:
    Variable not in scope: delete :: t1 -> t t1 -> t t1

What does this even mean?

This program only works when I change "(delete x ys)" to "ys", but then it doesn't do what it's supposed to do.

Any help would be great, thanks in advance.

3
  • 2
    Where is delete defined? Commented May 30, 2017 at 14:10
  • I was under the impression that that was a built-in function in Haskell: hackage.haskell.org/package/base-4.9.1.0/docs/… Commented May 30, 2017 at 14:17
  • By the way, your if can be rewritten in a more readable form as elem x ys && subset .... Usually, we tend to avoid ==False and if..then False in Haskell. Also tail xs looks wrong, are you sure you don't want xs instead ? Commented May 30, 2017 at 15:03

1 Answer 1

4

delete needs to be imported. According to https://www.haskell.org/hoogle/?hoogle=delete, you should add import Data.List to the beginning.

Besides, I guess your function subset is wrong. The result of subset [1, 2, 1] [1, 2, 3] is False.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I didn't know I had to import it. I think the result of that should be false though, since there is only a single 1 in the second list in your example
I don't know what should subset do, so I just guess it. I guessed wrong.
@Meurth A set can, mathematically speaking, not contain duplicates. In that case, sublist would probably the better name for your function, since lists/sequences can contain duplicates. from a mathematical point of view, {1, 1, 1} equals {1, 1} equals {1}.
@Polygnome Thanks for the reply, I suppose my teacher could learn something from that! :D

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.