Experienced Haskellers almost never use list indexing. I would use break to avoid repeated crawls (assuming you want to map element "4" and not index "3"):
case break (== 4) [1, 2, 3, 4, 5] of (a,x:xs) -> x:a ++ xs (a,xs) -> a ++ xs
How in:
Prelude Data.List> case break (== 4) [1, 2, 3, 4, 5] of (a,x:xs) -> x:a ++ xs; (a,xs) -> a ++ xs [4,1,2,3,5]
We can do the same with indexing via 'splitAt':
Prelude Data.List> case splitAt 3 [1, 2, 3, 4, 5] of (a,x:xs) -> x:a ++ xs; (a,xs) -> a ++ xs [4,1,2,3,5]
Don stewart
source share