Over the OCaml Group, Till Varoquaux asks if an arbitrary list can be tied over itself strictly, like you can do with recursive value definitions:

let rec arpeggio = 1 :: 2 :: 3 :: 2 :: arpeggio

In other words, a function `necklace`

is sought such that:

⟨∀l: |l| > 0 : ⟨∀k≥ 0 :: takek(necklacel) = takek(drop |l| (necklacel))⟩⟩

As Alain Frisch points out, it cannot be done safely, as only values of fixed shape (i.e., statically known) can be built recursively. However, with a parsimonious amount of unsafeness it can be done quite simply. I've written before of reversing trees in-place. The only operation needed is `rplacd`

, that replaces the tail of a list with another one. With that, the only care needed is to copy the original list before closing the loop:

let necklace = function | [] -> invalid_arg "necklace" | h :: t -> let rec r = h :: r in let p = ref r in List.iter (fun h -> let s = h :: r in ignore (rplacd !p s); p := s) t; r

Given a nonempty list, the code begins by building a singleton circular list on `r`. At each point, the code keeps a reference to the "tail" (i.e., the last node) of the circular list on `p`. With each further element of the original list, a new node pointing back to the beginning of the circular list is built on `s`, so that it both replaces and becomes the current tail.

Since at each step the cons cell is fresh, the original list is not modified and the circular list is built from scratch, cell by cell, always pointing back to itself. Hence the unsafe operation is completely hidden, and the code is safe.

## No comments:

Post a Comment