[Haskell-beginners] Complex list manipulation
legajid
legajid at free.fr
Tue Nov 10 16:09:36 EST 2009
Hello,
i'm trying to manipulate lists with a "complex" (two-level) structure :
a list containing tuples, containing a list containing tuples.
The object is to find a specific value according to 2 criterias.
Here is my code
tabmul= [ ( a, [ ( n, n*a ) | n <- [1..9] ] ) | a <- [2,3,4] ]
{- gives the following list
[
(2, [(1,2), (2,4), ....(9,18)]),
(3, [(1,3), (2,6), ....(9,27)]),
(4, [(1,4), (2,8), ....(9,36)])
]
-}
{- Get the result for a=3 and n=5 -}
-- select level 1 tuple for a=3
s1_flt (x,_) = (x==3)
s1=filter (s1_flt) tabmul -- get the list [(3, [(), () .......])]
s1_liste =head(s1) -- get the tuple (3, [.....])
-- extract the level 2 list of tuples
s1_tup (x,y)=y
-- then tuple for n=5
s1_flt2 (x, y) = x==5
s1_soltup = filter (s1_flt2) (s1_tup s1_liste) -- [(3,15)]
--finally result for 3 * 5
s1_sol1 (x, y)= y
s1_sol = s1_sol1 (head s1_soltup)
Perhaps the structure is not the most efficient for this example, but it
may simulate records in a database.
Getting the result seems really hard.
Do you know a shorter way to implement this search?
It probably would be simpler when i had triples (a, n ,a*n) ?
Another question : the values of criterias are hard-coded. What if i
would like to type in s1_sol 3 5; how to put these parameters in the
expressions for filters; the filters must get the parameters of the
function, in other words the function should return or generate filters ?
Thanks for helping
Didier.
More information about the Beginners
mailing list