2013-05-01 72 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