Skip to content

Instantly share code, notes, and snippets.

@killerswan
Forked from mneedham/gist:941892
Created April 26, 2011 07:04
Show Gist options
  • Save killerswan/941923 to your computer and use it in GitHub Desktop.
Save killerswan/941923 to your computer and use it in GitHub Desktop.
Purely Functional Data Structures - Chris Okasaki
(* This uses the @ operator (aka List.append) which is not tail recursive *)
let suffixes list =
let rec loop l acc =
match l with
| [] -> acc
| x::xs -> loop xs (acc @ [xs]) in
loop list [list]
(* This uses an operator @+ constructed from two tail recursive methods... :P *)
let suffixesTR list =
let (@+) x y = List.rev_append (List.rev x) y in
let rec loop l acc =
match l with
| [] -> acc
| x::xs -> loop xs (acc @+ [xs]) in
loop list [list]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment