[Haskell-beginners] Defined list data type compared to Haskell List

Lawrence Bottorff borgauf at gmail.com
Tue Jan 26 21:49:26 UTC 2021


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 <toad3k at 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 at 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 at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
> _______________________________________________
> Beginners mailing list
> Beginners at 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/3f4e6f0f/attachment.html>


More information about the Beginners mailing list