compare lists in Haskell

二次信任 提交于 2019-12-08 10:13:36

问题


I've been trying to compare two lists in Haskell and found an answer here
I wonder how
all (flip elem listx) input
works especially for the role flip plays here.
When I take out flip it won't work anymore.


回答1:


  1. flip elem listx is equivalent to (flip elem) listx.
  2. (flip elem) is the same as elem, but with the arguments in opposite order. This is what flip does.
  3. elem is a function that takes an element and a list, and checks whether the element belongs to the list.
  4. So flip elem is a function that that takes a list and an element, and checks whether the element belongs to the list.
  5. Therefore flip elem listx is a function that that takes an element, and checks whether the element belongs to listx.
  6. Now all takes a predicate and a list, and checks whether all elements of the list satisfy the predicate.
  7. all (flip elem listx) take a list, and checks whether all elements of the list satisfy flip elem listx. That is, whether they all belong to listx.
  8. all (flip elem listx) input checks whether all elements of input belong to listx.
  9. Q.E.D.


来源:https://stackoverflow.com/questions/7619217/compare-lists-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!