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. Re: Defined list data type compared to Haskell List (David McBride) 2. As pattern, @ (Lawrence Bottorff) 3. Re: As pattern, @ (Ut Primum) ---------------------------------------------------------------------- Message: 1 Date: Tue, 26 Jan 2021 18:14:27 -0500 From: David McBride <toa...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <Beginners@haskell.org> Subject: Re: [Haskell-beginners] Defined list data type compared to Haskell List Message-ID: <CAN+Tr432iej=QdpNKRgXgobe45=n88gauq55locsrqtkoml...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" You can, you have to write it slightly differently. But defined that way it can only ever have one element because NList only takes one. mylist = NList (NList (NList (Elem 1))) You could also change it to data NestedList a = Elem a | NList a (NestedList a) But at that point you have a nonempty list type (always at least one element) which already exists. On Tue, Jan 26, 2021, 17:40 Lawrence Bottorff <borg...@gmail.com> wrote: > Why can I not do this? > > data NestedList1 a = Elem a | NList (NestedList1 a) > > that is, with parens rather than square brackets, then > > myList2 = (NList (Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem 5))) > > It gives the error > > <interactive>:383:18-73: error: > ,* Couldn't match expected type `NestedList1 a' > with actual type `(NestedList1 Integer, NestedList1 a0)' > ,* In the first argument of `NList', namely > `(Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem 5))' > In the expression: > (NList (Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem 5))) > In an equation for `myList2': > myList2 > = (NList (Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem > 5))) > ,* Relevant bindings include > myList2 :: NestedList1 a (bound at <interactive>:383:1) > > etc., etc. > > On Tue, Jan 26, 2021 at 4:05 PM David McBride <toa...@gmail.com> wrote: > >> Right that is a plain list of NestedLists. So if you were to rewrite [a] >> as (Regularlist a) so to speak (not a real type), the definition of >> NestedList would be List (RegularList (NestedList a)). >> >> Keep in mind that List is a constructor, everything after it is types. >> >> On Tue, Jan 26, 2021 at 4:50 PM Lawrence Bottorff <borg...@gmail.com> >> wrote: >> >>> So NestedList is using regular List? So in >>> >>> data NestedList a = Elem a | List [NestedList a] >>> >>> the second data constructor List [NestedList a] we see a "regular" list >>> because of the square brackets? >>> >>> On Tue, Jan 26, 2021 at 3:42 PM David McBride <toa...@gmail.com> wrote: >>> >>>> In NestedList, the List constructor takes a regular list of >>>> NestedLists. Therefore when pattern matching on it you can get access to >>>> those nested lists. In your code, x is the first NestedList, and xs is the >>>> rest of the NestedLists. >>>> >>>> On Tue, Jan 26, 2021 at 4:32 PM Lawrence Bottorff <borg...@gmail.com> >>>> wrote: >>>> >>>>> I'm following this <https://wiki.haskell.org/99_questions/Solutions/7> >>>>> and yet I see this solution >>>>> >>>>> data NestedList a = Elem a | List [NestedList a] deriving (Show) >>>>> >>>>> flatten1 :: NestedList a -> [a] >>>>> flatten1 (Elem a ) = [a] >>>>> flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) >>>>> flatten1 (List []) = [] >>>>> >>>>> What I find puzzling is this line >>>>> >>>>> flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) >>>>> >>>>> where I see >>>>> >>>>> (List (x:xs)) as an argument. How is the NestedList type also able to >>>>> be expressed as a normal consed list with x:xs argument? How is (:) >>>>> interacting with NestedList? >>>>> >>>>> LB >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners@haskell.org >>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners@haskell.org >>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners@haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210126/12f59732/attachment-0001.html> ------------------------------ Message: 2 Date: Wed, 27 Jan 2021 00:01:23 -0600 From: Lawrence Bottorff <borg...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] As pattern, @ Message-ID: <cafahfsxnzndskgppg-__cvghnohp2ucljrspzg-1gmgsryc...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" I'm looking at this <https://wiki.haskell.org/99_questions/Solutions/8> and wondering how the "as pattern" actually is working compress (x:ys@(y:_)) | x == y = compress ys | otherwise = x : compress ys compress ys = ys I'm sure it's just some version of my stab at eliminating consecutive duplicates in a list compress :: Eq a => [a] -> [a] compress [] = [] compress [x] = [x] compress (x:y:xs) = if x == y then compress (y:xs) else x : compress (y:xs) only smarter. Could someone walk me through the (x:ys@(y:_)) part? LB -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210127/b893147d/attachment-0001.html> ------------------------------ Message: 3 Date: Wed, 27 Jan 2021 07:14:35 +0100 From: Ut Primum <utpri...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] As pattern, @ Message-ID: <CANjDmKKsFvgKLgLVz4n4W-_t_Edga+TnWh-bqbm9O=vanil...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hi, between the arguments of a function, ys@(y:yss) would mean that we call that argument ys, we call its head y and its tail yss. In your case the tail is replaced by _ , because is never used inside the function. Il mer 27 gen 2021, 07:02 Lawrence Bottorff <borg...@gmail.com> ha scritto: > I'm looking at this <https://wiki.haskell.org/99_questions/Solutions/8> > and wondering how the "as pattern" actually is working > > compress (x:ys@(y:_)) > | x == y = compress ys > | otherwise = x : compress ys > compress ys = ys > > I'm sure it's just some version of my stab at eliminating consecutive > duplicates in a list > > compress :: Eq a => [a] -> [a] > compress [] = [] > compress [x] = [x] > compress (x:y:xs) = if x == y then compress (y:xs) else x : compress (y:xs) > > only smarter. Could someone walk me through the (x:ys@(y:_)) part? > > LB > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210127/f9cdec9f/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 150, Issue 16 ******************************************