Haskell - List Comprehension - get Input-Elements -
i have problem list comprehension, if input list.
in these iii excercises it's not allowed use: map
, filter
, concat
!!!
part i
requirements:
a funktion f1
gets list xs
of tripels (a, b, cs)
a
, b
of type int
c
of type [int]
the function should generate list of pairs (a · b, b + c)
, c in cs , in generated list should appear pairs in 1st element bigger 2nd 1 - (a · b) > b + c
.
example:
f1 [(10,20,[1,10,100]), (4,5,[5,15,25])]
should return following list:
[(200,21),(200,30),(200,120),(20,10)]
my attempts:
f1 :: int -> int -> [int] -> [(int, int)] f1 b cs = [(a*b, b+c)| c<-cs, (a*b)>(b+c)]
it works fine, not lists input.
so tried several ways, unfortunately not right 1 :-(
f1 :: [(int, int, [int])] -> [(int, int)]
1st approach:
f1 xs = [((xs !! 0)*(xs !! 1), (xs !! 1)+c)| c<-(xs !! 2), ((xs !! 0)*(xs !! 1))>((xs !! 1)+c)]
2nd approach:
f1 let (a, b, cs) = xs = [(a*b, b+c)| c<-cs, (a*b)>(b+c)]
3rd approach:
f1 (a b cs) = [(a*b, b+c)| c<-cs, (a*b)>(b+c)]
all 3 don't work!
solution dave4420:
f1 :: [(int, int, [int])] -> [(int, int)] f1 xs = [ (a*b, b+c) | (a, b, cs) <- xs, c <- cs, (a*b)>(b+c) ]
part ii
requirements:
a function g1 gets list of pairs of same type , generate plain list out of it.
example:
g1 [(1,2),(3,4),(5,6)] returns [1,2,3,4,5,6]
my attempt:
g1 :: [(int, int)] -> [int] g1 xs = [a,b | (a,b)<-xs]
i compiling error because a,b in output of list comprehension not have correct syntax.
however can return or b or e.g. a+b:
g1 xs = [a | (a,b)<-xs]
or
g1 xs = [a+b | (a,b)<-xs]
could please me out of too?
thanks once again
part iii coming...
f1 :: [(int, int, [int])] -> [(int, int)] f1 xs = [ {-todo-} | (a, b, cs) <- xs, c <- cs, {-todo-} ]
i have left couple of {-todo-}s fill in.
part ii.
if had write
g1' :: [[int]] -> [int]
how that? can modify g1'
g1
desire?
Comments
Post a Comment