Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Syntax error using where with guards. (Manuel Vázquez Acosta)
   2. Re:  Syntax error using where with guards. (Francesco Ariis)
   3. Re:  Syntax error using where with guards. ([email protected])


----------------------------------------------------------------------

Message: 1
Date: Fri, 15 Sep 2017 18:53:54 -0400
From: Manuel Vázquez Acosta <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Syntax error using where with guards.
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

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.


------------------------------

Message: 2
Date: Sat, 16 Sep 2017 01:49:00 +0200
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Syntax error using where with guards.
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

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



------------------------------

Message: 3
Date: Fri, 15 Sep 2017 21:37:58 -0400
From: [email protected]
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Syntax error using where with guards.
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

Thanks, that solves it.  I really like `where`, though.


Francesco Ariis <[email protected]> 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
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 111, Issue 13
******************************************

Reply via email to