In common lisp, we can use the remove
function.
It seems there is no such a method in OCaml ?
In common lisp, we can use the remove
function.
It seems there is no such a method in OCaml ?
Lists in OCaml are immutable. So you can't remove things from them. You normally create another list that doesn't have the things you don't want. For this, you would use List.filter
.
If you absolutely have to have mutable lists, you can. In Batteries there is something called a Dllist that might be like what you want. (It is a doubly linked list, however, unlike a Lisp list).
One of the great things about OCaml, in my opinion, is that the pure functional subset is really quite effective. I've never needed to use mutable lists in my own projects.
List.filter
stop on removing one element? Maybe I have copies of elements that look the same, but only want to remove one. –
Nisa filteri
and filter the item with the index of the element you want to remove. –
Nisa below codes returns a new list that remove elements that are equal with specified 'v' from list 'l'.
let remove l v equal =
let rec aux l' v acc =
match l' with
| None -> acc
| hd :: tl ->
if equal hd v then
aux tl v acc
else
aux tl v (hd :: acc)
in
aux l v []
© 2022 - 2024 — McMap. All rights reserved.
filter
the list? (remove
, at least in the Common Lisp sense is a very, very scary beast... start/end ranges, counts, keys, and predicates, oh my!) – Propeller