2012-11-30 21 views
0

我已經開始學習Haskell和我有問題,瞭解清單的名單如何笛卡爾積工作Undestanding對Haskell的笛卡爾乘積執行如何工作

這裏是假設代碼

cprod = foldr f [[ ]] 
     where f xs yss = foldr g [ ] xs 
      where g x zss = foldr h zss yss 
        where h ys uss = (x : ys) : uss 

究竟我不明白的是最後一個功能 我把它換成變量名按我的理解

mycart = foldr f [[]] 
    where f currentresult listelem = foldr g [] currentresult 
      where g currentresultonstep currentresultelem = foldr h currentresultelem listelem 
       where h currentresultelemonstep onelistelem = (currentresultonstep:currentreslteleemonstep):onelistelem 

應該不是最後一個字符串是像這樣?

where h currentresultelemonstep onelistelem = (onelistelem:currentresultelemonstep):currentresultonstep 

,我們嘗試將列表中的元素添加到開始對當前結果 的要素是什麼?

回答

3

首先,作爲寫你的代碼在語法上不正確的:

foo.hs:3:13: parse error on input `where' 

其次,好像你感到困惑的論點的foldr第一個參數的順序:

foldr :: (a -> b -> b) -> b -> [a] -> b 

第一個參數(a)是輸入列表([a])的一個元素,第二個參數(b)是累加器。

+0

當我寫'where f x y',對於foldr,y - 是累加器? – Herokiller

+0

是的,x是當前元素。 – melpomene

+0

哦,我怎麼會認爲第一個參數是累加器,謝謝 – Herokiller