Finding index of element in a list in Haskell?

前端 未结 5 543
有刺的猬
有刺的猬 2021-01-03 18:36

I have a function in Haskell which finds the maximum value of an exponentiation from a list:

prob99 = maximum $ map (\\xs -> (head xs)^(head (tail xs))) n         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 19:14

    import Data.List
    elemIndex 'b' "abc" === Just 1
    

    A really good tool for finding haskell functions is Hoogle. Allows you to search by type signature among other things.

    If you wanted to do everything in one pass I'd recommend Data.List.mapAccumL, passing the index of the largest number found so far along as the accumulator.

提交回复
热议问题