2013-05-01 63 views
0

我有這樣的代碼,這是組合的公式,而重複:哈斯克爾,返回一個元組

combinaciones :: Int ->[Int]->[[Int]] 
combinaciones 0 _ = [[]] 
combinaciones _ [] = [] 
combinaciones k (x:xs) = [x:ys | ys <- combinaciones (k - 1) xs] ++ combinaciones k xs 

combinationsN :: Int ->Int->[[Int]] 
combinationsN n k = combinaciones k [1..n] 

我的問題是,我想返回一個列表的列表與列表中的列表中的號碼,一對夫婦:([[Int]],Int)。我怎樣才能做到這一點?

+1

你正在尋找的詞是「元組」。 – 2013-05-01 23:04:24

+0

是的!哈哈,你知道我該如何解決這個問題? :-( – mguedez 2013-05-01 23:05:26

+0

這是教程級的材料:http://en.wikibooks.org/wiki/Haskell/YAHT/Language_basics#Pairs.2C_Triples_and_More – millimoose 2013-05-01 23:09:38

回答

1

下面是簡單的版本 - 通過計數可能更有效地做到這一點,但這是我可以在凌晨1點提出的。

編輯:好的,讓我試着寫智能版本。

combinaciones :: Int -> [Int] -> ([[Int]], Int) 
combinaciones 0 _ = ([[]], 1) 
combinaciones _ [] = ([], 0) 
combinaciones k (x:xs) = 
    let (first, firstN) = combinaciones (k - 1) xs 
     (second, secondN) = combinaciones k xs 
    in ([x:ys | ys <- first] ++ second, firstN + secondN) 

絕對沒有保證,這是做正確的事情,我半睡半醒。 ;-)

+0

哈哈哈哈謝謝!它告訴我這一點:表達式中的語法錯誤(意外的'(' ),我看不出在哪裏錯過了一個括號! – mguedez 2013-05-01 23:26:16

+0

忘記它!它的工作!謝謝很多兄弟:-) – mguedez 2013-05-02 01:24:56