Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

How to use the map function correcly, to apply a function to every element in a list in Haskell?

I have a quation about the following method.

So my goal is to output every Data Node (or leaf) of this MultTree:

data MultTree a = Indexnode a a [MultTree a] | Datanode a deriving Show

What I have so far is this:

dataList :: MultTree a -> [a]
dataList (Indexnode _ _ []) = []
dataList (Datanode a) = [a]
dataList (Indexnode _ _ (x:y:z)) = (dataList x) ++ (dataList y)

This will output the first two elements. And I understand why. But I'd like to output every element. So I suppose, I have to apply the function dataList to every element of the childList, kind of like this:

dataList (Indexnode _ _ childList) = (dataList childlist)

Of course this particular example will not work. I also know that, in order to apply a fuction to every element of a list, I should use the map function. And I have. But apparantly I'm too dumb to actually apply it. Whatever it is, it doesn't work.

I'd be more than grateful, if someone could show me, how to use the map function correctly in this case, or if I'm totally wrong and map is not the right way to go, what else I can do.

Thank you a lot in advance!

question from:https://stackoverflow.com/questions/65861037/how-to-use-the-map-function-correcly-to-apply-a-function-to-every-element-in-a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...