Last active
March 25, 2021 09:23
-
-
Save gabyfle/7b3fbe9299cd0d5cc98357f9b7550bc2 to your computer and use it in GitHub Desktop.
Useless algorithms on lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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