Haskell : Minimum position -


i'm beginner in haskell terms. have exercise shows minimum positions. example: [1,2,3,1,1] => 0,3,4 these minimum positions. tried in 2 ways, these doesn't work. please can me recursive version?

first:

findpos :: [int]->int->[int] findpos list minimum (list) = [index | (index, e) <- zip [0..] list, e == minimum (list)] 

second:

findpos :: [int]->int->[int] findpos list (minim el) = [index | (index, e) <- zip [0..] list, e == (minim el)]   minim :: [int] -> int minim x = if (tail(x)==[]) head(x)       else n        n = minim (tail(x))             n = if n < head(x) n             else head(x) 

the serious problem code have strange on left of function definitions.

in first case have:

findpos :: [int] -> int-> [int] findpos list minimum (list) = … 

what mean? judging signature gave findpos, takes list , element looking for. the definition has be:

findpos list m = [index | (index, e) <- zip [0..] list, e == m] 

and in case there no need call minim function on right hand side, instead you’ll have call minim before calling findpos pass minimal element this:

findpos' :: [int] -> [int] findpos' list = findpos list (minim list) 

or can change definition to:

findpos :: [int] -> [int] findpos list = [index | (index, e) <- zip [0..] list, e == minim list] 

and in case call minim on right hand side, signature of findpos becomes different.


now recursive minimum function. should use pattern matching as possible:

minim :: [int] -> int minim (x : []) = x minim (x : xs) = if x < m x else m       m = minim xs 

otherwise minim correct except fact define n twice.


Comments