Pergunta

I'm trying to create an insertion sort method that allows me to do an insertion sort based on a function of an item, eg function applied to 1 could be greater than function applied to 2 or vice versa depending on the function.

I have this so far to work out if I can insert an element into a part of a list.

insertBy :: Ord b => (a -> b) -> a -> [a] -> [a] 
insertBy f a [] = [a]
insertBy f a (x:xs) = if ( (f a)< (f x )) then a:x:xs else insertBy f a xs

However, I'm getting a parse error on the second line. Sorry if it's really obvious but I can't see it.

Once I have that part I will call an insertion sort function that uses that to sort a list but first I need help with this :(

edit: exact error "Parse error in Pattern insertBy"

Foi útil?

Solução

I think you want the final expression to be

x : insertBy f a xs

Otherwise you're discarding the first element of the list in that case.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top