Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote