Search a list: Difference between revisions
Content added Content deleted
(→{{header|J}}: correction) |
(Adding Haskell) |
||
Line 82: | Line 82: | ||
4 Bush |
4 Bush |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
|||
Libraries and data: |
|||
import Data.List |
|||
haystack=["Zig","Zag","Ronald","Bush","Krusty","Wally","Charlie","Bush","Bozo"] |
|||
needles = ["Washington","Bush"] |
|||
I use 'lambda' notation for readability. |
|||
:Find 'just' an index: |
|||
*Main> map (\x -> (x,findIndex (==x) haystack)) needles |
|||
[("Washington",Nothing),("Bush",Just 3)] |
|||
Want to know if there are there more Bushes hiding in the haystack? |
|||
*Main> map (\x -> (x,findIndices (==x) haystack)) needles |
|||
[("Washington",[]),("Bush",[3,7])] |
|||
To be complete. Here is the 'point free' version of the task: |
|||
import Control.Monad |
|||
import Control.Arrow |
|||
*Main> map (ap (,) (flip findIndex haystack . (==))) needles |
|||
[("Washington",Nothing),("Bush",Just 3)] |
|||
=={{header|J}}== |
=={{header|J}}== |