Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Created August 19, 2016 11:34
Show Gist options
  • Select an option

  • Save MikeMKH/be82f72c1037aba81a46a9aeefdd0698 to your computer and use it in GitHub Desktop.

Select an option

Save MikeMKH/be82f72c1037aba81a46a9aeefdd0698 to your computer and use it in GitHub Desktop.
Coin Changer kata in F# using List.mapFold
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="FSharp.Core"
publicKeyToken="b03f5f7f11d50a3a"
culture="neutral"/>
<bindingRedirect oldVersion="4.3.1.0"
newVersion="4.4.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
namespace CoinChanger
module Changer =
let map coins amount =
List.mapFold
(fun amount coin -> (amount / coin, amount % coin))
amount
coins
|> fst
namespace CoinChanger.Tests
module ChangerTests =
open Xunit
open Swensen.Unquote
open CoinChanger.Changer
[<Fact>]
let ``given a full register and 0 cents it must return 0 coins`` () =
test <@ map [25; 10; 5; 1] 0 = [0; 0; 0; 0] @>
[<Fact>]
let ``given a full register and 99 cents it must return 3 quarters, 2 dimes, 0 nickels, and 4 pennies`` () =
test <@ map [25; 10; 5; 1] 99 = [3; 2; 0; 4] @>
@MikeMKH

MikeMKH commented Aug 19, 2016

Copy link
Copy Markdown
Author

I did not really TDD this out, I just want to revisit the List.mapFold function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment