Write a simple Haskell function elementPosition :: Eq t => t -> [t] -> Int eleme
ID: 3732516 • Letter: W
Question
Write a simple Haskell function
elementPosition :: Eq t => t -> [t] -> Int
elementPosition takes an element elt and a list lst, and returns the position of
the rst occurrence of elt in lst. That is, it returns the one-based index of the rst
occurrence of elt on lst. If elt does not appear in the list lst it should return 0. For
example:
elementPosition 3 [1,2,3,4,5] should return 3
elementPosition 'e' "elephant" should return 1
elementPosition 'p' "elephant" should return 4
elementPosition 'z' "elephant" should return 0
Explanation / Answer
import Data.List (elemIndex)
elementPosition :: Eq t => t->[t]->Int
elementPosition :: elt lst =
case elt 'elemIndex' lst of
Just n -> n+1
Nothing -> 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.