ers,
我试图通过ocaml和CYK表学习函数式编程,因此没有List.mem或任何必要的函数。我的目标是形成2个细胞的产物。
以下是我目前的情况:
let stringlister = function(mystring, newlist) ->
List.append newlist mystring;;
let rec append_func = function([listleft;listright], anslist, i, j) ->
if (j == (List.length listright)) then anslist
else begin
append_func([listleft;listright], anslist, i, j + 1);
List.append(anslist (stringlister((List.nth listright j), (stringlister( (List.nth listleft i), [])))))
end;;
let rec prod_func = function([listleft;listright], anslist, i, j) ->
if (i == (List.length listleft)) then anslist
else begin
prod_func([listleft;listright], anslist, i + 1, j);
append_func([listleft;listright], anslist, i, j)
end;;
let product = function[listleft;listright] ->
if (listleft == [] || listright == []) then []
else prod_func([listleft;listright], [], 0, 0);;
预期的输出应该是这样的:
#product[["A";"B"];["D","E","F","G"]];;
-: string list = ["AD"; "AE"; "AF"; "AG"; "BD"; "BE"; "BF"; "BG"]
#product[["A","B"];[]];;
-: string list = []
我的思维过程是制作一系列递归函数,基本上循环遍历列表,将每个字符串与另一个列表中的每个字符串放在一起。
我认为我的错误是我要追加的方式,特别是在append_func中。我认为更好的问题可能是如何创建字符串列表。
答案 0 :(得分:1)
我是Ocaml的新手,所以也许有不同的方式
let rec flat_map f xs =
match xs with
| [] -> []
| x :: xs -> List.append (f x) (flat_map f xs);;
val flat_map : ('a -> 'b list) -> 'a list -> 'b list = <fun>
let product lists =
let rec loop acc lists =
match lists with
| [] -> [[]]
| first :: [] -> first |> List.map (fun x -> x :: acc)
| first :: rest -> first |> flat_map (fun x -> loop (x :: acc) rest)
in
loop [] lists;;
val product : 'a list list -> 'a list list = <fun>
product [["A"; "B"]; ["D"; "E"; "F"; "G"]]
- : string list list =
[["D"; "A"]; ["E"; "A"]; ["F"; "A"]; ["G"; "A"]; ["D"; "B"]; ["E"; "B"];
["F"; "B"]; ["G"; "B"]]
当然它适用于任何数量的输入列表
product [["1"; "2"; "3"]; ["A"; "B"; "C"; "D"]; ["+"; "-"]];;
- : string list list =
[["+"; "A"; "1"]; ["-"; "A"; "1"]; ["+"; "B"; "1"]; ["-"; "B"; "1"];
["+"; "C"; "1"]; ["-"; "C"; "1"]; ["+"; "D"; "1"]; ["-"; "D"; "1"];
["+"; "A"; "2"]; ["-"; "A"; "2"]; ["+"; "B"; "2"]; ["-"; "B"; "2"];
["+"; "C"; "2"]; ["-"; "C"; "2"]; ["+"; "D"; "2"]; ["-"; "D"; "2"];
["+"; "A"; "3"]; ["-"; "A"; "3"]; ["+"; "B"; "3"]; ["-"; "B"; "3"];
["+"; "C"; "3"]; ["-"; "C"; "3"]; ["+"; "D"; "3"]; ["-"; "D"; "3"]]
也许他们使用function
let rec flat_map f = function
| [] -> []
| x :: xs -> List.append (f x) (flat_map f xs)
let product lists =
let rec loop acc = function
| [] -> [[]]
| first :: [] -> first |> List.map (fun x -> x :: acc)
| first :: rest -> first |> flat_map (fun x -> loop (x :: acc) rest)
in
loop [] lists
我们也可以从另一个角度来解决问题。注意输出顺序的差异
let product lists =
let rec loop acc = function
| [] -> acc
| first :: rest -> loop acc rest |> flat_map (fun c -> List.map (fun x -> x :: c) first)
in
loop [[]] lists;;
val product : 'a list list -> 'a list list = <fun>
product [["1"; "2"; "3"]; ["A"; "B"; "C"; "D"]; ["+"; "-"]];;
- : string list list =
[["1"; "A"; "+"]; ["2"; "A"; "+"]; ["3"; "A"; "+"]; ["1"; "B"; "+"];
["2"; "B"; "+"]; ["3"; "B"; "+"]; ["1"; "C"; "+"]; ["2"; "C"; "+"];
["3"; "C"; "+"]; ["1"; "D"; "+"]; ["2"; "D"; "+"]; ["3"; "D"; "+"];
["1"; "A"; "-"]; ["2"; "A"; "-"]; ["3"; "A"; "-"]; ["1"; "B"; "-"];
["2"; "B"; "-"]; ["3"; "B"; "-"]; ["1"; "C"; "-"]; ["2"; "C"; "-"];
["3"; "C"; "-"]; ["1"; "D"; "-"]; ["2"; "D"; "-"]; ["3"; "D"; "-"]]
以上flat_map
会为列表中的每个元素调用昂贵的List.append
。下面的变体收集中间结果,然后通过一次调用List.concat
let flat_map f xs =
let rec loop k = function
| [] -> k []
| x :: xs -> xs |> loop (fun r -> k (f x :: r))
in
loop List.concat xs;;
val flat_map : ('a -> 'b list) -> 'a list -> 'b list = <fun>
答案 1 :(得分:1)
使用Monads(monads for functionnal programming)可以简化您的代码。
module ListMonad =
struct
type 'a t = 'a list
let return x = [x]
let bind l f = List.fold_right (fun x acc -> (f x)@acc) l []
let zero = []
let ( >>= ) l f = bind l f
end;;
首先,一个基本用例:
["A";"B"] >>= fun (x ->
[["C"];["D"]] >>= fun y -> x::y);;
它返回2列表的产品:[["A";"C"];["A";"D"];["B";"C"];["B";"D"]]
完整的用例(列表列表的产品),我们使用List.fold:
List.fold_right (fun x acc -> product x acc)
[["a";"b"];["c";"d";"e"];["f";"g"]] [[]];;
将产生:
[["a"; "c"; "f"]; ["a"; "c"; "g"]; ["a"; "d"; "f"]; ["a"; "d"; "g"];
["a"; "e"; "f"]; ["a"; "e"; "g"]; ["b"; "c"; "f"]; ["b"; "c"; "g"];
["b"; "d"; "f"]; ["b"; "d"; "g"]; ["b"; "e"; "f"]; ["b"; "e"; "g"]]
`
答案 2 :(得分:0)
想象一下嵌套循环的C语言。 此外,我们的想法是从尾部开始循环遍历第二个列表。把它放在第一个列表的另一个循环中,从尾部开始。第一轮将到达两个列表的末尾,并且您希望此返回一个空列表。然后它将开始回溯两个列表的最后一个元素。您想要返回的元素是第一个带有第二个列表头的列表头连接。这与您刚刚创建的列表相同。之所以从尾部开始是因为列表是不可变的,只需在列表前面添加一个新头就不那么容易了。您的函数有一个带有两个列表的参数。然而,它不是你想要的列表,它是列表中的内容,这是箭头左侧,两个列表的头部和尾部。现在请记住,你循环遍历第二个列表,循环遍历第一个列表,然后以相反的顺序连接头部。