Skip to content

Instantly share code, notes, and snippets.

@ntreu14
Last active January 18, 2018 22:50
Show Gist options
  • Save ntreu14/8e0896b672bdc4bcce5211bc5da5ca22 to your computer and use it in GitHub Desktop.
Save ntreu14/8e0896b672bdc4bcce5211bc5da5ca22 to your computer and use it in GitHub Desktop.
Implementations of fold and reduce functions in F#
let rec fold f state list =
match list with
| [] -> state
| head::rest -> fold f (f state head) rest
let reduce f list =
match list with
| [] -> invalidArg "list" "Cannot pass an empty list"
| head::rest -> fold f head rest
let concat = sprintf "%s%s"
let strs = ["Example"; "Strings"; "To"; "Join"]
printfn "%s" <| reduce concat strs
printfn "%s" <| fold concat System.String.Empty strs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment