Finding index of element in a list in Haskell?

前端 未结 5 546
有刺的猬
有刺的猬 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:24

    This probably doesn't deserve to be in an answer of his own, but I can't comment yet. Anyway, here is how I would have written this:

    import Data.List
    import Data.Ord
    
    maxIndex ::  Ord a => [a] -> Int
    maxIndex = fst . maximumBy (comparing snd) . zip [0..]
    

提交回复
热议问题