0
有人加入#haskell
,並問一個相當新手的作業問題。你如何排序兩個等長的字符串元組列表?爲了幫助他們 - 我的Haskell很糟糕 - 我寫了這個。類型統一問題與字符串元組列表
sortBy (\(x:y) (x':y') -> let { a = x `compare` x'; b = y `compare` y' } in if a == EQ then b else a) $ let f (a,b) = a++b in f ([("a", "b"), ("e", "b"), ("x", "b"), ("x", "g")], [("b", "c"),("b", "d"), ("g", "a"), ("g", "c")])
我認爲這遠遠沒有工作。爲什麼這一工作並不:
sortBy (\(x:y) (x':y') -> undefined) $ [("a","b"),("e","b"),("x","b"),("x","g"),("b","c"),("b","d"),("g","a"),("g","c")]
我得到這個錯誤
<interactive>:1:67:
Couldn't match expected type `[t]'
against inferred type `([Char], [Char])'
In the expression: ("a", "b")
In the expression: [("a", "b"), ("e", "b"), ("x", "b"), ("x", "g")]
In the first argument of `f', namely
`([("a", "b"), ("e", "b"), ("x", "b"), ("x", "g")],
[("b", "c"), ("b", "d"), ("g", "a"), ("g", "c")])'
我把這些多條線路上,但我不知道,我必須打破他們,使他們的工作(Haskell空白是愚蠢的)。
SortBy有一個類型的sortBy :: (a -> a -> Ordering) -> [a] -> [a]
和我的名單有一個類型的[([Char], [Char])]
爲什麼a
不能([Char], [Char])
統一做出
sortBy :: (([Char], [Char]) -> ([Char], [Char]) -> Ordering) -> [([Char], [Char])] -> [([Char], [Char])]`
我可能會遺漏一些東西,但一看就好像你定義的比較函數就是從'Ord'實例中爲'([Char] ,[Char])',對吧? – 2010-11-09 23:40:48
耶穌基督。有史以來最棒的評論。非常真實。它是爲元組定義的非常酷。 – 2010-11-10 01:49:19