0

I am trying to get the cartesian product of two lists and I am getting an error that says that my function productHelp is not defined. I am not too sure how to solve this without having a helper function and I am not even sure if this would even work if it'd let me call my helper function.

let rec product A B =
    match A, B with
    | [], [] -> [ [] ]
    | e1::rest1, [] -> productHelp A B :: product rest1 B

let rec productHelp A B =
    match A, B with
    | [], [] -> [ [] ]
    | _, [] -> [ [] ]
    | e1::rest1, e2::rest2 -> [e1::e2]::productHelp A rest2


/home/codio/workspace/program/set/set.fs(144,24): error FS0039: The value or constructor 'productHelp' is not defined. Maybe you want one of the followi
ng:   product [/home/codio/workspace/program/set/set.fsproj]
    5 Warning(s)
    1 Error(s)

Example:

// Example:
//   A = [1;2]
//   B = [3;4]
//   ==> [ [1;3]; [1;4]; [2;3]; [2;4] ]
// 
Shinji-san
  • 971
  • 4
  • 14
  • 31

2 Answers2

3

In F#, you must order your declarations within a file so that a declaration only refers to the ones above it. That is, declare the helper before the product function.

Jonas Høgh
  • 10,358
  • 1
  • 26
  • 46
0

What about using a comprehension?

let product A B = [for a in A do for b in B -> [a; b]]
gileCAD
  • 2,295
  • 1
  • 10
  • 10