let empty_list = [];; let one_element_list = 3::empty_list;; let one_element_list = "test"::empty_list;;
1 2 3 4 5 6
# let empty_list = [];; val empty_list : 'a list = [] # let one_element_list = 3::empty_list;; val one_element_list : int list = [3] # let one_element_list = "test"::empty_list;; val one_element_list : string list = ["test"]
letrec drop_value l to_drop = match l with | []-> [] | to_drop::tl -> drop_value tl to_drop (* 绑定新的to_drop *) | hd::tl -> hd::drop_value tl to_drop;; (* 警告没有使用该match *) drop_value [1;2;3;4;5;] 5;; (* 由于非空都走了to_drop::tl 全删除了 *)
1 2 3 4 5 6 7 8 9 10 11 12
# let rec drop_value l to_drop = match l with | []-> [] | to_drop::tl -> drop_value tl to_drop | hd::tl -> hd::drop_value tl to_drop;; Characters 103-109: | hd::tl -> hd::drop_value tl to_drop;; ^^^^^^ Warning 11: this match case is unused. val drop_value : 'a list -> 'a -> 'a list = <fun> # drop_value [1;2;3;4;5;] 5;; - : int list = []
使用常规语句
1 2 3 4 5 6 7 8 9 10
letrec drop_value l to_drop = match l with | [] -> [] | hd::tl -> let new_tl = drop_value tl to_drop in(* 使用常规的if而不是模式匹配来判断 *) if hd = to_drop then new_tl else hd::new_tl ;; drop_value [1;2;3;4;5;] 5;;
1 2 3 4 5 6 7 8 9 10 11 12
# let rec drop_value l to_drop = match l with | [] -> [] | hd::tl -> let new_tl = drop_value tl to_drop in (* 使用常规的if而不是模式匹配来判断 *) if hd = to_drop then new_tl else hd::new_tl;; val drop_value : 'a list -> 'a -> 'a list = <fun> # drop_value [1;2;3;4;5;] 5;; - : int list = [1; 2; 3; 4]