2014-11-16 13 views
3

我是ocaml的新手,嘗試編寫一些代碼以生成兩個值之間的所有數字列表。生成兩個值(OCaml或其他語言)之間給定長度的所有列表

例如,如果我調用該函數generate,我想獲得這樣的事:

let generate ~min ~max ~length (* Maybe other arguments *) = 
    (* The code *) 
;; 

generate ~min:0 ~max:3 ~length:4;; 

應該返回

[ 
[0;0;0]; 
[1;0;0]; 
[2;0;0]; 
[3;0;0]; 
[0;1;0]; 

等等,以

[3;2;3]; 
[0;3;3]; 
[1;3;3]; 
[2;3;3]; 
[3;3;3]; 
] 

我已經試過這樣的代碼:

open Core.Std;; 

type next_list = 
    | Complete of int list 
    | Partial of int list 
    | Result of (int array) list 
;; 

let rec next ~r ~min ~max ~l = 
    let detox = function | Partial a -> a | _ -> assert false in 
    match l with 
    | Partial (hd :: tl) when hd <= max -> Partial (hd + 1 :: tl) 
    | Partial (hd :: tl) when hd = max + 1 -> next ~r ~min ~max 
    ~l:(Partial (min :: (detox (next ~r ~min ~max ~l:(Partial tl))))) 
    | Complete (hd :: tl) when hd <= max -> next ~r:([l] :: r) ~min ~max 
    ~l:(Complete (hd + 1 :: tl)) 
    | Complete (hd :: tl) when hd = max + 1 -> next ~r ~min ~max 
    ~l:(Complete (min :: (detox (next ~r ~min ~max ~l:(Partial tl))))) 
    (*| Partial [] -> next ~r ~min ~max ~l:(Result r)*) 
    | Result a -> Result a 

如果有必要,它可能會分散在幾個功能中,這不是問題。
我也對非ocaml代碼或想法感興趣。

感謝您的幫助。

這是我在Stackoverflow上的第一個問題,請不要猶豫,如果我的問題不清楚。

+0

我嘗試了幾個遞歸函數將頭部增加到最大值,然後追加下一個從尾部取出的列表。 – leowzukw

+0

http:// stackoverflow。com/questions/13998275/generate-all-lists-of-size-n-such-that-each-element-is-between-0-and-m-inclusi/ – didierc

回答

1

這裏一些溶液: 首先,讓我們定義需要2個列表L1 & L2作爲輸入,併產生列表,其中每個元素是L2通過L1的1個元件增強的列表:

let enumerate l ll = List.fold ~init:[] ~f:(fun op x -> (x::ll)::op) l;; 

枚舉[0; 1; 2; 3] [4; 5; 6] ;; - :int list list = [[3; 4; 5; 6]; [2; 4; 5; 6]; [1; 4; 5; 6]; [0; 4; 5; 6]]

立即產生:

let rec generate length ll = 
    if length=1 then List.fold ~init:[] ~f:(fun op x -> [x]::op) ll 
    else 
     let result = generate (length-1) ll in 
     List.fold ~init:[] ~f:(fun op x -> (enumerate ll x)@op) result;; 

和用法如下:

generate 2 [1;2;3];; (* instead of generate ~min:1 ~max:3 ~length:2 *) 

一些說明: List.fold〜INIT:[]〜F:(樂趣運算X - > [x] :: op)ll =>這將創建列表(單例)的初始列表

第二種:將長度爲-1的每個列表執行並執行枚舉。

+0

它工作得很好,謝謝! – leowzukw

0

這裏有一個提示:

let count_prefix low high lists = 
    ??? 

let generate min max length = 
    let rec recur low high len = 
    if len = 0 then [] 
    else count_prefix low high (recur low high (len - 1)) in 
    recur min max length 

count_prefix應該返回一個列表,是lists與數字lowhigh前綴的元素。如果lists爲空,則應返回包含數字lowhigh的列表的列表。即:

count_prefix 0 3 [] => [[0]; [1]; [2]] 
count_prefix 0 3 [[10];[20]] => [[0; 10]; [0; 20]; [1; 10]; [1; 20]; [2; 10]; [2; 20]] 

填寫count_prefix的定義。

相關問題