-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCrossProduct.hs
More file actions
39 lines (28 loc) · 931 Bytes
/
CrossProduct.hs
File metadata and controls
39 lines (28 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module CrossProduct where
import Data.List
{- |
'crossProduct' gets two lists and returns the cross product as tuple-list
-}
crossProduct :: [a] -> [b] -> [(a,b)]
crossProduct xs ys = [ (x,y) | x <- xs, y <- ys]
{- |
'crossProductAsList' gets two lists and returns the cross product as list
of lists
-}
crossProductAsList :: [t] -> [t] -> [[t]]
crossProductAsList xs ys = map (\(x,y) -> [x,y]) $ crossProduct xs ys
{- |
'crossProductAsSecond' gets a list of lists and a list and returns the
cross product
-}
crossProductAsSecond :: [[t]] -> [t] -> [[t]]
crossProductAsSecond xss ys = [ xs ++ [y] | xs <- xss, y <- ys ]
{- |
'crossProductOverList' gets a list of lists and returns the cross product
of the list
-}
crossProductOverList :: [[t]] -> [[t]]
crossProductOverList [] = []
crossProductOverList xs@(_:[]) = xs
crossProductOverList (x:y:ys) =
foldl crossProductAsSecond (crossProductAsList x y) ys