【发布时间】:2011-07-10 14:23:53
【问题描述】:
我的问题是转这个:
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
进入一个跟踪比较次数的解决方案,这里是我需要生成的代码骨架:
iSortCount :: Ord a => [a] -> (Integer, [a])
iSortCount [] = ...
iSortCount (x:xs) = ...
insCount x (k, []) = ...
insCount x (k, (y:ys)) -- Count the times when it reach's here
| x <= y = ...
| otherwise = ...
where ...
我已经尝试了很多事情,从使用 let、wheres、writer monad,创建自己的类型,state monad,我似乎只是在看一些东西,因为我一直遇到“y : ins”的问题x ys" 因为该函数返回的应该是 (Int, [a]) 并且 : 不适用于元组。我试图把它拆分来做这样的事情
do
(a,b) <- ins x (k+1, ys)
return (k, (y : b))
但它似乎不认为 ins 在那个版本中返回一个元组,所以我猜它只是不是模式匹配。我的主要问题是我现在应该看哪里?我为此工作了很长时间,这个问题开始让我感到沮丧,因为它看起来很容易......
在以斯拉的帮助下回答:
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
【问题讨论】:
-
一个 MVar 浮现在脑海中,但这似乎是一个杂牌。我很好奇别人会怎么说。
-
不能只写一个函数
cmp x y = {-# SCC "cmp" #-} (x <= y),用cmp代替<=和ghc的profiling吗?还是您实际上需要程序中可用的比较次数,而不仅仅是分析结果?
标签: sorting haskell monads counting