Haskell Cartesian Product Recursively -
this question has reply here:
cartesian product 9 answersi know how utilize list comprehension this, how can implement function recursively compute cartesian product given 2 sets?
here's i'm stuck (and i'm noob)
crossprod :: [int] -> [int] -> [(int,int)] crossprod xs ys | xs == [] || ys == [] = [] | otherwise = (head xs, head ys) : crossprod (tail xs) (ys)
the output of gives me
[(1,4),(1,5),(1,6)]
if sets [1,2,3]
, [4,5,6]
respectively.. how go getting rest?
i know guard
, , if else
please bare me.
the basic case this:
{-crossprodaux :: int -> [int] -> [(int,int)]-} crossprodaux x [] = [] crossprodaux x (a:b) = (x, a):(crossprodaux x b) {-crossprod :: [int] -> [int] -> [(int,int)]-} crossprod [] ys = [] crossprod (a:b) ys= (crossprodaux ys)++(crossprod b ys)
haskell cartesian-product
No comments:
Post a Comment