learning F#, it seems pretty cool, favourite functional language so far.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeerSong.fs 1.1KB

12345678910111213141516171819202122232425262728293031
  1. module BeerSong
  2. let stateLine n =
  3. if n > 1
  4. then sprintf "%d bottles of beer on the wall, %d bottles of beer." n n
  5. else if n = 1
  6. then "1 bottle of beer on the wall, 1 bottle of beer."
  7. else "No more bottles of beer on the wall, no more bottles of beer."
  8. let takeDownLine n =
  9. if n > 2
  10. then sprintf "Take one down and pass it around, %d bottles of beer on the wall." (n - 1)
  11. else if n = 2
  12. then "Take one down and pass it around, 1 bottle of beer on the wall."
  13. else if n = 1
  14. then "Take it down and pass it around, no more bottles of beer on the wall."
  15. else "Go to the store and buy some more, 99 bottles of beer on the wall."
  16. let verse (n: int) (left: int) =
  17. if left > 1 then [ stateLine n; takeDownLine n; "" ] else [ stateLine n; takeDownLine n ]
  18. let rec beerLines (n: int) (left: int): list<string> =
  19. let newBottles = if n = 0 then 99 else n - 1 // wrap around when get to 0
  20. if left > 0 && n >= 0 then
  21. List.concat
  22. [ (verse n left)
  23. (beerLines newBottles (left - 1)) ]
  24. else
  25. []
  26. let recite (startBottles: int) (takeDown: int) = beerLines startBottles takeDown