3

我想要长度大于getIndex xs y的第一个子列表的索引。xsy

输出是:

[[],[4],[4,3],[3,5,3],[3,5,5,6,1]]
aufgabe6: <<loop>>

为什么getIndex不工作?

import Data.List

-- Die Sortierfunktion --
myCompare a b
    | length a < length b = LT
    | otherwise = GT

sortList :: [[a]] -> [[a]]
sortList x = sortBy myCompare x

-- Die Indexfunktion --
getIndex :: [[a]] -> Int -> Int
getIndex [] y = 0
getIndex (x:xs) y
    | length x <= y = 1 + getIndex xs y
    | otherwise = 0
    where (x:xs) = sortList (x:xs)

main = do
    print (sortList [[4],[3,5,3],[4,3],[3,5,5,6,1],[]])
    print (getIndex [[4],[3,5,3],[4,3],[3,5,5,6,1],[]] 2)
4

3 回答 3

9

让它终止

问题是在这种情况下

getIndex (x:xs) y
    | length x <= y = 1 + getIndex xs y
    | otherwise = 0
    where (x:xs) = sortList (x:xs)

你混淆了哪个(x:xs)是哪个。你应该改为

getIndex zs y
    | length x <= y = 1 + getIndex xs y
    | otherwise = 0
    where (x:xs) = sortList zs

给予

Main> main
[[],[4],[4,3],[3,5,3],[3,5,5,6,1]]
3
*Main> getIndex [[],[2],[4,5]] 1
2
*Main> getIndex [[],[2],[4,5]] 5
3

这为您提供了至少在排序列表中的第一个长度列表的数量,这实际上回答了“原始y列表中最多有多少个列表?”的问题。y

我们怎样才能找到其他事实?

如果您想要原始列表中的位置,您可以使用它们的位置标记条目,使用zip

*Main> zip [1..] [[4],[3,5,3],[4,3],[3,5,5,6,1],[]]
[(1,[4]),(2,[3,5,3]),(3,[4,3]),(4,[3,5,5,6,1]),(5,[])]

让我们创建一个实用函数来处理这些:

hasLength likeThis (_,xs) = likeThis (length xs)

我们可以这样使用它:

*Main> hasLength (==4) (1,[1,2,3,4])
True
*Main> filter (hasLength (>=2)) (zip [1..] ["","yo","hi there","?"])
[(2,"yo"),(3,"hi there")]

这意味着现在很容易编写一个函数,为您提供第一个长度大于 的列表的索引y

whichIsLongerThan xss y = 
    case filter (hasLength (>y)) (zip [1..] xss) of
         [] -> error "nothing long enough" -- change this to 0 or (length xss + 1) if you prefer
         (x:xs) -> fst x

这给了我们

*Main> whichIsLongerThan [[4],[3,5,3],[4,3],[3,5,5,6,1],[]] 2
2
*Main> whichIsLongerThan [[4],[3,5,3],[4,3],[3,5,5,6,1],[]] 3
4
*Main> whichIsLongerThan [[4],[3,5,3],[4,3],[3,5,5,6,1],[]] 0
1

更短?

但我们可以做类似的技巧:

whichIsShorterThan xss y = 
    case filter (hasLength (<y)) (zip [1..] xss) of
         [] -> error "nothing short enough" -- change this to 0 or (length xss + 1) if you prefer
         (x:xs) -> fst x

所以你得到

*Main> whichIsShorterThan [[4],[3,5,3],[4,3],[3,5,5,6,1],[]] 2
1
*Main> whichIsShorterThan [[4],[3,5,3],[4,3],[3,5,5,6,1],[]] 1
5
*Main> whichIsShorterThan [[4],[3,5,3],[4,3],[3,5,5,6,1],[]] 0
*** Exception: nothing short enough

广义的

让我们拉出那里的共同主题:

whichLength :: (Int -> Bool) -> [[a]] -> Int
whichLength likeThis xss = 
    case filter (hasLength likeThis) (zip [1..] xss) of
         [] -> error "nothing found" -- change this to 0 or (length xss + 1) if you prefer
         (x:xs) -> fst x

所以我们可以做

*Main> whichLength (==5) [[4],[3,5,3],[4,3],[3,5,5,6,1],[]] 
4
*Main> whichLength (>2) [[4],[3,5,3],[4,3],[3,5,5,6,1],[]] 
2
于 2013-05-20T00:34:27.180 回答
1

你的意思是index of the firs sublist with length > y?如果这不是目标(并且< y是),那么

length x <= y = 1 + getIndex xs y

应该

length x >= y = 1 + getIndex xs y

另外,(x:xs) = sortList (x:xs)底部,因为它永远不会结束。如果您想以某种方式对其进行排序,则可以遵循 AndrewC 的解决方案。

于 2013-05-20T00:34:53.260 回答
0

letHaskell 中的(和where)绑定是递归的:LHS 和 RHS 都属于(并因此引用)同一个新范围。您的代码相当于

........
getIndex (x:xs) y = 
  let                  -- new, extended scope, containing definitions for 
      (x:xs) = a       --   new variables x, xs, a ... the original vars x, xs
      a = sortList a   --   are inaccessible, __shadowed__ by new definitions
  in                   -- evaluated inside the new, extended scope
    if length x <= y         -- x refers to the new definition
      then 1 + getIndex xs y
      else 0

当 的值x被 要求时length,其新定义(x:xs) = a要求 的值a,该值直接根据自身定义,a = sortList a

a = sortList a
  = sortList (sortList a)
  = sortList (sortList (sortList a))
  = sortList (sortList (sortList (sortList a)))
  = ....

一个黑洞

于 2013-05-22T08:22:19.693 回答