Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell "parse error in pattern" when multiplying 2 with x in a given tuple condition

I have a tuple study that I want to write out in Haskell. It's given that S = {〈x, y〉| 2x + y = 7, x ∈B, y ∈B }, where B = {1, 2.. 20}. I figured out that the answer is S = {〈1, 5〉,〈2, 3〉,〈3, 1〉}. However, when I type it out in a online Haskell compiler https://app.codingrooms.com/w/Yr2VJHj0yb9S, I either get way too many tuples, or I get an error message Snapshot, Haskell compiler . I've tried changing up the code by separating x and y, or by alterating between parenthesis and square brackets, but I haven't had any success. Does anyone have any advice for me? Thanks in advance!

Code in Haskell compiler:

b = [1, 2.. 20]
s = [(x,y)|x <- b, y <- b, let 2*x + y = 7]

main = do
print s
like image 596
Tanix Avatar asked Jan 30 '26 05:01

Tanix


1 Answers

Your let 2*x + y = 7 is actually short for let (+) (2*x) y = 7, so you are redefining the (+) function. But one of the parameters has as pattern (2*x), which is not a valid pattern. But even if that would work, it will produce tuples for the entire B×B space, so not the correct answer.

You can let x and y enumerate over the list B and filter with 2*x + y == 7:

f b = [ (x, y) | x <- b, y <- b, 2*x + y == 7 ]

but probably it is slightly more efficient to perform a membership check:

f b = [ (x, y) | x <- b, let y = 7 - 2*x, y `elem` b ]

both yield:

ghci> f [1 .. 20]
[(1,5),(2,3),(3,1)]
like image 95
Willem Van Onsem Avatar answered Feb 01 '26 20:02

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!