0
votes

For some reason, when I run the code with for exmaple let's say split1 [1,2,3,4,5,6,7,8,9,10] I get an error

p :: Int -> Bool
p x  = if x < 5 then True else False

split1 [xs] = [([x,y]) | x <- [xs], y <- [xs], p x == True, p y == False]

Even if I run it with split1 [1] I get an empty set. Can someone tell me where i'm wrong? Thanks.

1
As a style note, I'd avoid p x = if x<5 then True else False, and simply use the simpler equivalent p x = x<5. Similarly, p x == True is equivalent to p x, and p y == False is equivalent to not (p y). - chi

1 Answers

2
votes

When you say:

split1 [xs] = ...

You're essentially doing a pattern matching on first argument. [xs] is pattern for a list with only one element. What you need instead is this:

split1 xs = [([x,y]) | x <- xs, y <- xs, p x == True, p y == False]

Note that I removed list brackets around xs in the definition too.

I don't know what you're trying to do, but you may want to get rid of list brackets in ([x,y]) too.

split1 xs = [(x,y) | x <- xs, y <- xs, p x == True, p y == False]