微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

删除列表中两次出现之间的元素

如何解决删除列表中两次出现之间的元素

我必须创建一个函数,该函数接受一个列表并返回该列表,但在出现之间没有元素。

例如:[1;2;3;4;2;7;14;21 ;7;5] -> [1;2;7;5]

我想为了制作这个我会取列表的头部,然后看到 如果尾部有其他事件,那么我浏览列表,当我找到该事件时,我删除它们之间的所有内容,只保留其中一个

首先我尝试了这样的事情:

let rec remove list = match list with 
    | [] -> []
    | h::t -> if(List.mem h t) then
                (*Here I would like to go through the list element by element to
                 find the occurence and then delete everything between*)
               else
                 remove t 

因此,对于我没有成功完成的部分,我创建了一个函数,允许在两个给定点之间对列表进行切片,就像这样:

let slice list i k =
   let rec take n = function
     | [] -> []
     | h :: t -> if n = 0 then [] else h :: take (n-1) t
   in
   let rec drop n = function
     | [] -> []
     | h :: t as l -> if n = 0 then l else drop (n-1) t
   in
   take (k - i + 1) (drop i list);;

(*Use: slice ["a";"b";"c";"d";"e";"f";"g";"h";"i";"j"] 2 3;;*)

我也有这个函数可以让我获取列表中点的索引:

let index_of e l = 
  let rec index_rec i = function
    | [] -> raise Not_found
    | hd::tl -> if hd = e then i else index_rec (i+1) tl
  in
  index_rec 0 l ;;
(*Use: index_of 5 [1;2;3;4;5;6] -> return 4*)

但我真的不知道如何将它们结合起来得到我所期望的。 谢谢你能给我一些建议来帮助我。

解决方法

这是我做的:

let rec remove liste = 
    let rec aux l el = match l with 
    | [] -> raise Not_found
    | x :: xs -> if el = x then try aux xs el with Not_found -> xs 
        else aux xs el in 
    match liste with 
    | [] -> []
    | x :: xs -> try let r = x :: aux xs x in remove r with Not_found -> x :: remove xs;;

我的 aux 函数返回在 l 中最后一次出现 el 之后的列表。如果您有任何问题或需要更多解释,请在评论中问我

,

一个使用 option 类型来判断元素是否出现在列表更靠后的版本:

let rec find_tail ?(eq = (=)) lst elem =
  match lst with
  | x :: _ when eq x elem -> Some lst
  | _ :: xs -> find_tail ~eq xs elem
  | [] -> None

let rec remove ?(eq = (=)) lst =
  match lst with
  | [x] -> [x]
  | x :: xs -> begin
      match find_tail ~eq xs x with
      | Some tail -> x :: remove ~eq (List.tl tail)
      | None -> x :: remove ~eq xs
    end
  | [] -> []

还允许您指定比较函数(默认为 =)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。