Send Beginners mailing list submissions to beginners@haskell.org 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 beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org 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. (mva....@gmail.com) ---------------------------------------------------------------------- Message: 1 Date: Fri, 15 Sep 2017 18:53:54 -0400 From: Manuel Vázquez Acosta <mva....@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Syntax error using where with guards. Message-ID: <87r2v74lq5....@pavla.com> 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 <fa...@ariis.it> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Syntax error using where with guards. Message-ID: <20170915234900.4iaz6q2umv7dx...@x60s.casa> 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: mva....@gmail.com To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Syntax error using where with guards. Message-ID: <87lglf4e4p....@pavla.com> Content-Type: text/plain; charset=utf-8 Thanks, that solves it. I really like `where`, though. Francesco Ariis <fa...@ariis.it> 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 > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 111, Issue 13 ******************************************