Haskell list comprehension compilation error

104 views Asked by At

I want to create a function that given two lists (of floats, but could be anything) gives a list with all the combinations of taking two elements from the first list and putting it in the last and all the combinations of the former putting one of the elements from the last (now with more elements) and putting it back on the first.

For every movement it also gives the greatest value that moved (in the second movement only one moves, that is the value). It should all end when the first list is empty (there would be no second movement).

I can't expect anyone to understand that so, examples:

next [1,2,3,4] [] -> [(([1,2], [3,4], 4), ([1,2,3],[4],3)), (([1,2], [3,4], 4), ([1,2,4],[3],4)), (([1,3], [2,4], 4), ([1,2,3],[4],2)), (([1,3], [2,4], 4), ([1,3,4],[2],4)).....
next [1,2] [3,4]  -> [(([], [1,2,3,4],2),)], ())

What I have so far:

module Test where 

next :: [Float] -> [Float] -> [(([Float],[Float], Float),([Float],[Float], Float))]
next [] _               = []
next (a:b:[]) s     
    |a>b                = [([],a:b:s, a)]
    |otherwise          = [([],a:b:s, b)]
next d s                = [([x,z], i:j:s, j), b | i <- d, j <- d, i < j, x <- d, z <- d, x < z, z /= i, z /= j, x /= z, x /= i, x /= j, b <- (back [x,z] i:j:s)]
    where
        back d s        = [(i:d, [x,z], i) | i <- s, x <- s, z <- s, x < z, z /= i, x /= z]

Compiling that code gives back an error on the first comprehension list at the | character. I'd kind of understand an error in the back function. Why won't that compile?

1

There are 1 answers

1
undur_gongor On BEST ANSWER

The comprehension syntax is

[ <expression> | ... ]

For <expression> you have ([x,z], i:j:s, j), b which is syntactically wrong. Did you mean ([x,z], i:j:s, b)?