Я уже искал в Интернете, у меня есть Real World Haskell, но я не могу понять, как напечатать список списков целых чисел, когда этот список возвращается combinatoricsGeneration.combination.
Я нашел модуль вhttp://www.polyomino.f2s.com/david/haskell/combinatorics.html Функции не имеют сигнатуры типа, поэтому Haskell должен делать все выводы.Я даже пытался добавить подписи, но ошибка все еще есть (более конкретная).
Исходный код, который я использую из этого модуля:
combinationsOf 0 _ = [[]]
combinationsOf _ [] = []
combinationsOf k (x:xs) = map (x:) (combinationsOf (k-1) xs)
++ combinationsOf k xs
combinations k n = combinationsOf k [1..n]
Я добавил следующие подписи, чтобы увидетьесли это что-то изменило, но это не так:
combinationsOf :: Integer -> [a] -> [[a]]
combinations :: Integer -> Integer -> [[Integer]]
Мой исходный файл на Haskell:
module Main
where
import IO
import qualified CombinatoricsGeneration as CG
main = putStr $ unlines $ map show CG.combinations(6, 8)
Компиляция CombinatoricsGeneration в порядке.Но один из моих исходных файлов выдает ошибку:
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.12.3
$ ghc -c CombinatoricsGeneration.hs
$ ghc -o test CombinatoricsGeneration.o test.hs
test.hs:6:37:
Couldn't match expected type `[a]'
against inferred type `t -> t1 -> [[t1]]'
In the second argument of `map', namely `CG.combinations'
In the second argument of `($)', namely
`map show CG.combinations (6, 8)'
In the second argument of `($)', namely
`unlines $ map show CG.combinations (6, 8)'
Однако следующая строка отлично работает:
main = putStr $ unlines $ map show [[1,2],[2],[3]]
Не могли бы вы помочь мне отобразить этот простой список?
1024 * ТИА *