functional programming - Haskell all valuations for variables given domain of values -



functional programming - Haskell all valuations for variables given domain of values -

for assignment have create haskell function which, given list of (string, [integer]) (where integer list represents domain of values string) tuples gives possible combinations of strings , integers.

so example, input valuations [ ("firstvar", [1..3]), ("secondvar", [1,2]) ] should yield:

[ [("firstvar", 1), ("secondvar", 1)], [("firstvar", 1), ("secondvar", 2)], [("firstvar", 2), ("secondvar", 1)], [("firstvar", 2), ("secondvar", 2)], [("firstvar", 3), ("secondvar", 1)], [("firstvar", 3), ("secondvar", 2)] ]

and should work n lists. far, i've made work 2 lists, i'm still having problem higher-order functions, hence confused how should create work n lists.

how did 2 lists through function valuations:

valuations :: (string, [integer]) -> (string, [integer]) -> [[(string, integer)]] valuations (a, bs) (c, ds) = pairvaluations (makelists(a, bs)) (makelists(c, ds)) pairvaluations :: -> -> [a] pairvaluations xs ys = [ [x, y] | x <- xs, y <- ys] makelists :: (string, [integer]) -> [(string, integer)] makelists (a, bs) = [(a, b) | b <- bs]

then valuations ("firstvar", [1..3]) ("secondvar", [1,2]) indeed give desired result. i'm having troubles expand functionality multiple lists. hope can help me out.

my solution utilize sequence:

valuations = sequence . map makelists

including @Ørjan johansen's solutions do:

valuations = mapm makelists

or, data.traversable:

valuations = traverse sequencea

haskell functional-programming

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -