My problem is to do this:
iSort :: Ord a => [a] -> [a]
iSort [] = []
iSort (x:xs) = ins x (iSort xs)
ins x [] = [x]
ins x (y:ys)
| x <= y = x : y : ys
| otherwise = y : ins x ys
To the solution that tracks the number of comparison attempts, here is the skeleton of the code I need to create:
iSortCount :: Ord a => [a] -> (Integer, [a])
iSortCount [] = ...
iSortCount (x:xs) = ...
insCount x (k, []) = ...
insCount x (k, (y:ys)) -- Count the times when it reach here
| x <= y = ...
| otherwise = ...
where ...
I tried a lot of what the let, wheres, monad writer used, creating my own type, state monad, and I seem to just redefine something because I keep running into the problem: "y: ins x ys", because what this function returns must be (Int, [a]) and: does not work on the tuple. I tried to break it to do something like this
do
(a,b) <- ins x (k+1, ys)
return (k, (y : b))
, , , ins , , . - ? , , ...
Ezra:
iSort' [] = []
iSort' (x:xs) = ins' x (iSort' xs)
ins' x [] = [x]
ins' (x,i) (y:ys)
| x <= fst y = (x,i+1) : y : ys
| otherwise = y : ins' (x,i+1) ys
countInsertions x = sum $ map snd $ iSort' $ zip x $ repeat 0