Skip to content

Instantly share code, notes, and snippets.

@gabyfle
Last active March 25, 2021 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabyfle/7b3fbe9299cd0d5cc98357f9b7550bc2 to your computer and use it in GitHub Desktop.
Save gabyfle/7b3fbe9299cd0d5cc98357f9b7550bc2 to your computer and use it in GitHub Desktop.
Useless algorithms on lists
let begaie b =
let rec begaie_aux l = function
| [] -> l
| h :: t -> begaie_aux (h :: h :: l) t
in
List.rev (begaie_aux [] b)
(* sans utiliser List.rev (et donc sans la version récursive terminale)
let rec begaie = function
| [] -> failwith "Liste vide"
| h :: t -> h :: h :: (begaie t)
*)
let begaie_crescendo l =
let rec repeat h acc = function
| 0 -> acc
| n -> repeat h (h :: acc) (n - 1)
in
let rec begaie_aux n l = match l with
| [] -> l
| h :: t -> (repeat h [] n) @ (begaie_aux (n + 1) t) (* <- this is evil (@ with a non tail-recursive function) *)
in
begaie_aux 1 l
let de_bagaie l =
let rec de_bagaie_aux x acc = function
| [] -> acc
| h :: t when h = x -> de_bagaie_aux x acc t
| h :: t -> de_bagaie_aux h (h :: acc) t
in
match l with
| [] -> l
| h :: t -> List.rev (de_bagaie_aux h [h] l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment