2014-10-05 59 views
1

我有這個簡單的方法是Haskell,其中分配太多的解釋:比實際的不同類型和希望的類型 - 哈斯克爾

-- IgnoreAfter problem: ignoreAfter 3 [7,8,9,3,4,5] == [7,8,9]. 
ignoreAfter 0 xs = [] 
ignoreAfter n (x:xs) = if length((x:xs)) >= n then 
          x : ignoreAfter(n-1 xs) 
         else 
          [] 

,我發現了以下錯誤:

pattern_matching.hs:19:32: 
    Couldn't match expected type `[a0]' with actual type `[a0] -> [a0]' 
    In the return type of a call of `ignoreAfter' 
    Probable cause: `ignoreAfter' is applied to too few arguments 
    In the second argument of `(:)', namely `ignoreAfter (n - 1 xs)' 
    In the expression: x : ignoreAfter (n - 1 xs) 
Failed, modules loaded: none. 

雖然我知道邏輯是健全的,我無法弄清楚我在這裏失去了什麼......有人可以幫助我嗎?

+0

如果答案符合您的需求,請不要忘記接受答案(點擊白色複選標記)。它讓其他人知道這個問題已經被正確回答了,它會給你2分以接受答案,並且給予提供答案的人15分:-) – user224567893 2014-10-05 17:39:31

+0

你能想出一個更有效的方法來做到這一點嗎? '長度'是O(n),所以你現在的算法很慢! – dfeuer 2014-10-05 17:59:30

回答

4

變化x : ignoreAfter(n-1 xs)變爲x : ignoreAfter (n-1) xs。 ()不是Haskell中的函數應用程序的一部分。當您通過(n-1 xs)ignoreAfter時,它將它視爲只有一個參數。這就是您收到Probable cause: ignoreAfter is applied to too few arguments消息的原因。

+0

tchaaaaa - 謝謝! – cybertextron 2014-10-05 16:54:23