learning F#, it seems pretty cool, favourite functional language so far.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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