2011-11-04 54 views
1

我在與獲取和在遞歸函數從列表中拋棄了第一值的問題,請刪除。哈斯克爾:獲取並從列表

下面是代碼:

removeDuplicates a u = [if a == [] then u else removeDuplicates newA newU 
         | let newU = (head a):u 
         | let newA = tail a] 

和錯誤:

Illegal parallel list comprehension: use -XParallelListComp 

而另一個想法:

removeDuplicates a u = [if a == [] then u else removeDuplicates (tail a) newU 
         | let newU = (head a):u] 

而另一個錯誤:

Occurs check: cannot construct the infinite type: a0 = [a0] 
    Expected type: [a0] 
     Actual type: [[a0]] 
    In the return type of a call of `removeDuplicates' 
    In the expression: removeDuplicates (tail a) newU 

提前致謝。

編輯:此時,只有我想用這個功能做的事情是從第一個列表由一個移動的所有項目到第二遞歸函數之一。之後,我會添加更多的東西來從列表中刪除重複值。

+0

你能更清楚它是什麼你想要做什麼? – Apocalisp

+0

你想使用列表解析還是讓..在子句中?要通過一個移動的元素之一,@Apocalisp的代碼做這項工作 – nponeccop

+0

的'ParallelListComp'是通過使用'造成[... | ... | ...]'。除非你喜歡做某事,否則總是'[... | ...]'。 – 2011-11-04 21:18:43

回答

1

它看起來就像你正在試圖做的:

removeDuplicates [] u = u 
removeDuplicates (x:xs) u = removeDuplicates xs (x:u) 

但這基本上是reverse

reverse = foldl (flip (:)) [] 
+0

我要downvote,然後我閱讀完整的問題:-) – Simon

0

除非你做功課或學習Haskell中,存在Data.Listnub功能。

+0

是的,我知道nub的功能,是的,它適用於學校,我不能使用它。 – Trac3

+0

難道是欺騙從'nub'源解除代碼?無論是不是,爲了學習,在查看「書後」之前,您應該嘗試自己實現它。 –

+0

OK,下次還會添加標籤功課..剛剛完成我的功能,Haskell是真正偉大的語言,但它是很難習慣吧:) – Trac3

0

原因你得到列表理解和「無限類型:A0 = [ a0]「錯誤是因爲編譯器正在讀取括號作爲定義列表。使用語法

f x = a + b 
     where a = 10 
      b = 20 

f' x = let a = 3 
      b = 4 
     in a + b