[Haskell-beginners] Syntax error using where with guards.

Manuel Vázquez Acosta mva.led at gmail.com
Fri Sep 15 22:53:54 UTC 2017


I was trying to implement a functional version of numerator/denominator
simplifications of symbols.

I came up with:

    simplify :: (Eq o, Ord o) => [o] -> [o] -> ([o], [o])
    simplify [] bs = ([], bs)
    simplify ts [] = (ts, [])
    simplify (x:xs) (y:ys)
      | x == y    = simplify xs ys
      | x < y     =
          ([x] ++ (fst $ simplify xs (y:ys)), snd $ simplify xs (y:ys))

      | otherwise =
          (fst r, [y] ++ snd r) where r = simplify (x:xs) ys


    simplify2 :: (Eq o, Ord o) => [o] -> [o] -> ([o], [o])
    simplify2 a b = simplify (sort a) (sort b)


    -- sort not shown here

In the second guard of the simplify function the expression 'simplify xs
(y:ys)' is repeated.  I would like to write instead:

    simplify (x:xs) (y:ys)
      | x == y    = simplify xs ys
      | x < y     =
          ([x] ++ fst r, snd r) where r = simplify xs (y:ys)

      | otherwise =
          (fst r, [y] ++ snd r) where r = simplify (x:xs) ys


But doing so is parse error:

    $ ghc xoutil/dim/meta.hs
    [1 of 1] Compiling Meta             ( xoutil/dim/meta.hs, xoutil/dim/meta.o )

    xoutil/dim/meta.hs:19:3: parse error on input ‘|’


I'm using: ghc 7.10.3.

Any ideas of why this is a syntax error?

Best regards,
Manuel.


More information about the Beginners mailing list