Syntax error using where with guards.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Syntax error using where with guards.

Manuel Vázquez Acosta
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.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Syntax error using where with guards.

Francesco Ariis
On Fri, Sep 15, 2017 at 06:53:54PM -0400, Manuel Vázquez Acosta wrote:
>     $ 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 ‘|’

Hello Manuel,
    `where` scopes over several guarded equations (and this is the main
difference with `let`). This should do the trick:

    | a == 1 = let x = 10
               in x + a
    | a == 2 = let x = 3
               in x + a

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Syntax error using where with guards.

Manuel Vázquez Acosta
Thanks, that solves it.  I really like `where`, though.


Francesco Ariis <[hidden email]> writes:

> On Fri, Sep 15, 2017 at 06:53:54PM -0400, Manuel Vázquez Acosta wrote:
>>     $ 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 ‘|’
>
> Hello Manuel,
>     `where` scopes over several guarded equations (and this is the main
> difference with `let`). This should do the trick:
>
>     | a == 1 = let x = 10
>                in x + a
>     | a == 2 = let x = 3
>                in x + a
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners