I'm trying to traverse through a list of lists and perform an operation on each list.
If I have a list like
[[("vT",T),("vF",T)],[("vT",T),("vF",F)],[("vT",F),("vF",T)],[("vT",T),("vF",M)],[("vT",M),("vF",T)],[("vT",M),("vF",M)],[("vT",F),("vF",F)],[("vT",F),("vF",M)],[("vT",M),("vF",F)]]
How can I create a function that sets an x = [("vT",T),("vF",T)]
, so that I can use this x to perform an operation, and then set x = [("vT",T),("vF",F)]
and so on and then have the answer of each returned in a list?
You can use map
to apply a function to each element of a list - here's a complete example:
data Tribool = T | F | M deriving Show
my_data = [
[("vT",T),("vF",T)],
[("vT",T),("vF",F)],
[("vT",F),("vF",T)],
[("vT",T),("vF",M)],
[("vT",M),("vF",T)],
[("vT",M),("vF",M)],
[("vT",F),("vF",F)],
[("vT",F),("vF",M)],
[("vT",M),("vF",F)]
]
f x = reverse x -- example function
processed_data = map f my_data
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.