Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access elements of a custom List data type?

I created a custom List type and I’m trying to implement the zip function for it. But I cant figure it out, it always throw an error on the last line.

data List a = Empty | Cons a (List a) deriving (Eq, Ord, Show, Read)

listZip :: List a -> List a -> List a
listZip _ Empty = Empty
listZip Empty _ = Empty
listZip (Cons x1 (x2)) (Cons y1 (y2)) = Cons (Cons x1 y1) (listZip x2 y2)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The return type looks wrong, you probably want to return a list of 2-tuples, so:

listZip :: List a -> List a -> List (a, a)

or more generic:

listZip :: List a -> List b -> List (a, b)

Then you implement this with:

listZip :: List a -> List b -> List (a, b)
listZip _ Empty = Empty
listZip Empty _ = Empty
listZip (Cons x1 (x2)) (Cons y1 (y2)) = Cons … (listZip x2 y2)

where I leave the part as an exercise.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading