Skip to content

Instantly share code, notes, and snippets.

@alexpw
Created October 28, 2014 17:03
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 alexpw/1179f5211c2e80e0b5ff to your computer and use it in GitHub Desktop.
Save alexpw/1179f5211c2e80e0b5ff to your computer and use it in GitHub Desktop.
pfds 2.1 suffixes sml
(* use "suffix.sml"; *)
(* input = output *)
(* [1,2,3] = [[1,2,3], [2,3], [3], []] *)
(* std recursion *)
fun suffixes ([]) = [[]]
| suffixes (xs) = xs :: suffixes (tl xs);
(* iterative / tail-recursive version *)
local
fun helper ([], rs) = rs
| helper (xs, rs) = helper (tl xs, rs @ [xs])
in
fun isuffixes (xs) = helper (xs, []) @ [[]]
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment