Created
October 28, 2014 17:03
-
-
Save alexpw/1179f5211c2e80e0b5ff to your computer and use it in GitHub Desktop.
pfds 2.1 suffixes sml
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
(* 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