Last active
January 18, 2018 22:50
-
-
Save ntreu14/8e0896b672bdc4bcce5211bc5da5ca22 to your computer and use it in GitHub Desktop.
Implementations of fold and reduce functions in F#
This file contains 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
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