Last active
August 3, 2016 14:18
-
-
Save c-cube/92fee8e668b01a303d4f34042ed3869a to your computer and use it in GitHub Desktop.
permutations using Sequence
This file contains hidden or 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
| #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