Skip to content

Instantly share code, notes, and snippets.

@c-cube
Last active August 3, 2016 14:18
Show Gist options
  • Select an option

  • Save c-cube/92fee8e668b01a303d4f34042ed3869a to your computer and use it in GitHub Desktop.

Select an option

Save c-cube/92fee8e668b01a303d4f34042ed3869a to your computer and use it in GitHub Desktop.
permutations using Sequence
#require "sequence";;
open Sequence.Infix;;
let rec perms = function
| [] -> Sequence.return []
| x :: tail -> perms tail >>= insert x
and insert x l = match l with
| [] -> Sequence.return [x]
| y :: tail ->
Sequence.cons (x :: l) (insert x tail >|= fun tail' -> y :: tail')
;;
perms [1;2;3;4] |> Sequence.to_list;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment