Parsing list of list haskell -
hello have function given list returns subset of permutations of it. want make function given list of list produces list of lists using first function. more clear:
delete x [] = [] delete x (y:xs) = if (x==y) (delete x xs) else (y:delete x xs) insert x n [] = [] insert x n xs = take (length xs - n) xs ++ [x] ++ drop (length xs - n) xs insert_and_delete x n xs= [insert x n (delete x xs)] my_permutation x 0 list = insert_and_delete x 0 list my_permutation x n list = insert_and_delete x n list ++ my_permutation x (n-1) list --n lenght of list
my_permuation 5 4 [5,1,1,1] [[5,1,1,1],[5,1,1,1],[1,5,1,1],[1,1,5,1],[1,1,1,5]] my_permutation 3 3 [3,1,1] [[3,1,1],[1,3,1],[1,1,3]]
now want make function given list of lists eg [[5,1,1,1] , [3,1,1]]
return list containing results above :
[[5,1,1,1],[5,1,1,1],[1,5,1,1],[1,1,5,1],[1,1,1,5],[3,1,1],[1,3,1],[1,1,3]]
my try far:
generate_permutations2 [xs:list] = my_permutation xs (length(xs:list)) (xs:list) ++ generate_permutations2 [list]
but when try call get:
generate_permutations2[ [2,1,1], [3,1]] exception: non-exhaustive patterns in function generate_permutations2
edit if change aguments , add base case:
generate_permutations2 [[]] = [[]] generate_permutations2 (xs:list) = my_permutation xs (length(xs:list)) (xs:list) ++ generate_permutations2 [list]
occurs check: cannot construct infinite type: t0 = [t0] expected type: [t0] actual type: [[t0]] in expression: list in first argument of
generate_permutations2', namely
[list]' in second argument of(++)', namely
generate_permutations2 [list]'
the compiler complaining generate_permutations2 doesn’t handle case input empty list. add clause generate_permutations2 []
did insert
, delete
.
editorial:
your intentions lot clearer if added type annotations functions. ghc lot better @ getting root of problem when tell top-level types. plus, other haskell programmers used inferring you’re doing type signatures.
Comments
Post a Comment