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. take until duplicate (mike h) 2. Re: take until duplicate (mike h) 3. Re: take until duplicate (Francesco Ariis) 4. Ambogous error in returning value (Baa) ---------------------------------------------------------------------- Message: 1 Date: Sun, 24 Sep 2017 20:08:40 +0100 From: mike h <mike_k_hough...@yahoo.co.uk> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] take until duplicate Message-ID: <9db6075e-0a0c-460f-8d9c-f58b2a1e8...@yahoo.co.uk> Content-Type: text/plain; charset=utf-8 I’m looking at how to take from a list until a duplicate is found. e.g. takeUntilDup [1,2,3,4,3] = [1,2,3,4] I found this implementation takeUntilDup = foldr (\x r -> x : takeWhile (/= x) r) [] It works but I can’t see how!!? The accumulated result is built up in r so with input [1,2,3,4,3] then, at the point when r = [1, 2, 3, 4], the fold is about to use the number 3. i.e. it does takeWhile (/=3) [1,2,3,4] which gives [1,2] Please, how does this work? Thanks Mike . ------------------------------ Message: 2 Date: Sun, 24 Sep 2017 20:31:46 +0100 From: mike h <mike_k_hough...@yahoo.co.uk> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] take until duplicate Message-ID: <e640d782-e673-422a-b508-f03517ad7...@yahoo.co.uk> Content-Type: text/plain; charset=utf-8 duh! I tried scanr (\x acc -> x : takeWhile (/= x) acc) [] [1,2,3,4,5,3] which gives [[1,2,3,4,5],[2,3,4,5],[3,4,5],[4,5,3],[5,3],[3],[]] which kind of makes sense. M > On 24 Sep 2017, at 20:08, mike h <mike_k_hough...@yahoo.co.uk> wrote: > > I’m looking at how to take from a list until a duplicate is found. > e.g. > > takeUntilDup [1,2,3,4,3] = [1,2,3,4] > > I found this implementation > > takeUntilDup = foldr (\x r -> x : takeWhile (/= x) r) [] > > It works but I can’t see how!!? > The accumulated result is built up in r so with input [1,2,3,4,3] then, at > the point when r = [1, 2, 3, 4], the fold is about to use the number 3. i.e. > it does takeWhile (/=3) [1,2,3,4] which gives [1,2] > > Please, how does this work? > > Thanks > Mike . ------------------------------ Message: 3 Date: Sun, 24 Sep 2017 21:34:10 +0200 From: Francesco Ariis <fa...@ariis.it> To: beginners@haskell.org Subject: Re: [Haskell-beginners] take until duplicate Message-ID: <20170924193410.tew655ltajkqi...@x60s.casa> Content-Type: text/plain; charset=us-ascii On Sun, Sep 24, 2017 at 08:31:46PM +0100, mike h wrote: > duh! I tried > > scanr (\x acc -> x : takeWhile (/= x) acc) [] [1,2,3,4,5,3] > > which gives > [[1,2,3,4,5],[2,3,4,5],[3,4,5],[4,5,3],[5,3],[3],[]] > > which kind of makes sense. > > M Well done, I find that :step and :show bindings are useful too in these cases. ------------------------------ Message: 4 Date: Mon, 25 Sep 2017 13:06:17 +0300 From: Baa <aqua...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] Ambogous error in returning value Message-ID: <20170925130617.2c745f7d@Pavel> Content-Type: text/plain; charset=UTF-8 Hello, everyone. Considering, I have a class: class Flt a where allows :: FltOpts -> [a] denies :: FltOpts -> [a] crit :: a -> [a] -> Bool flt :: FltOpts -> a -> Bool flt opts a = allowed && not denied where allowed = if null $ allows opts then True else a `crit` (allows opts) denied = if null $ denies opts then False else a `crit` (denies opts) I get error here: • Could not deduce (Flt a1) arising from a use of ‘allows’ from the context: Flt a bound by the class declaration for ‘Flt’ at .../.stack-work/intero/intero5319V42.hs:(31,1)-(38,97) The type variable ‘a1’ is ambiguous These potential instance exist: instance Flt MyType -- Defined at ... • In the second argument of ‘($)’, namely ‘allows opts’ .................................................... As I understand, GHC can not deduce type if it's a return's value (contraposition?). OK, but it knows its type: it is `[a]`! What is the problem to keep `flt` method as a generic, i.e. without concreate type, but only `[a]` ? Second, I implemented instance: instance Flt MyType where allows = ... denies = ... flt opts a = allowed && not denied where allowed = if null $ (allows opts::[MyType]) then True else a `crit` (allows opts) denied = if null $ (denies opts::[MyType]) then False else a `crit` (denies opts) and without this explicite type annotation of `allows opts` I get again ambigous error. But why? GHC knows that `allows` returns `[a]` and `a` is `MyType`, so `[a]` is `[MyType]`. Why I need to write it explicitly? May be I need some extension here? === Best regards, Paul ------------------------------ 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 16 ******************************************