Why can I not do this?data NestedList1 a = Elem a | NList (NestedList1 a)that is, with parens rather than square brackets, thenmyList2 = (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 <toad3k@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 <borgauf@gmail.com> wrote:So NestedList is using regular List? So indata 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 <toad3k@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 <borgauf@gmail.com> wrote:I'm following this 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 lineflatten1 (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