ONLY DO WHAT ONLY YOU CAN DO

こけたら立ちなはれ 立ったら歩きなはれ

F# で Project Euler Problem 17

> let mutable map:Map<int, string> = Map.empty;;

val mutable map : Map<int,string> = map []

> map <- map |> Map.add 1     "one";;
val it : unit = ()
> map <- map |> Map.add 2     "two";;
val it : unit = ()
> map <- map |> Map.add 3     "three";;
val it : unit = ()
> map <- map |> Map.add 4     "four";;
val it : unit = ()
> map <- map |> Map.add 5     "five";;
val it : unit = ()
> map <- map |> Map.add 6     "six";;
val it : unit = ()
> map <- map |> Map.add 7     "seven";;
val it : unit = ()
> map <- map |> Map.add 8     "eight";;
val it : unit = ()
> map <- map |> Map.add 9     "nine";;
val it : unit = ()
> map <- map |> Map.add 10    "ten";;
val it : unit = ()
> map <- map |> Map.add 11    "eleven";;
val it : unit = ()
> map <- map |> Map.add 12    "twelve";;
val it : unit = ()
> map <- map |> Map.add 13    "thirteen";;
val it : unit = ()
> map <- map |> Map.add 14    "fourteen";;
val it : unit = ()
> map <- map |> Map.add 15    "fifteen";;
val it : unit = ()
> map <- map |> Map.add 16    "sixteen";;
val it : unit = ()
> map <- map |> Map.add 17    "seventeen";;
val it : unit = ()
> map <- map |> Map.add 18    "eighteen";;
val it : unit = ()
> map <- map |> Map.add 19    "nineteen";;
val it : unit = ()
> map <- map |> Map.add 20    "twenty";;
val it : unit = ()
> map <- map |> Map.add 30    "thirty";;
val it : unit = ()
> map <- map |> Map.add 40    "forty";;
val it : unit = ()
> map <- map |> Map.add 50    "fifty";;
val it : unit = ()
> map <- map |> Map.add 60    "sixty";;
val it : unit = ()
> map <- map |> Map.add 70    "seventy";;
val it : unit = ()
> map <- map |> Map.add 80    "eighty";;
val it : unit = ()
> map <- map |> Map.add 90    "ninety";;
val it : unit = ()
> map <- map |> Map.add 1000  "one thousand";;
val it : unit = ()
> let numeral_string (n:int):string =
-     if Map.containsKey n map then
-         map.[n]
-     else
-         let mutable s = ""
-
-         // 100の位
-         let n1 =  n / 100
-         if n1 <> 0 then s <- map.[n1] + " hundred"
-
-         // 10の位 + 1の位
-         let n2 = n % 100
-         if (n1 <> 0 ) && (n2 <> 0) then s <- s + " and "
-         if (0 < n2) && (n2 <= 20) then
-             s <- s + map.[n2]
-         else
-             // 10の位
-             let n3 = n2 / 10
-             if (2 <= n3) then s <- s + map.[n3 * 10]
-
-             // 1の位
-             let n4 = (n2 % 10)
-             if (0 < n4) then s <- s + "-" + map.[n4]
-
-         s
- ;;

val numeral_string : int -> string
> [1..100]
- |> List.map(numeral_string)
- ;;
val it : string list =
  ["one"; "two"; "three"; "four"; "five"; "six"; "seven"; "eight"; "nine";
   "ten"; "eleven"; "twelve"; "thirteen"; "fourteen"; "fifteen"; "sixteen";
   "seventeen"; "eighteen"; "nineteen"; "twenty"; "twenty-one"; "twenty-two";
   "twenty-three"; "twenty-four"; "twenty-five"; "twenty-six"; "twenty-seven";
   "twenty-eight"; "twenty-nine"; "thirty"; "thirty-one"; "thirty-two";
   "thirty-three"; "thirty-four"; "thirty-five"; "thirty-six"; "thirty-seven";
   "thirty-eight"; "thirty-nine"; "forty"; "forty-one"; "forty-two";
   "forty-three"; "forty-four"; "forty-five"; "forty-six"; "forty-seven";
   "forty-eight"; "forty-nine"; "fifty"; "fifty-one"; "fifty-two";
   "fifty-three"; "fifty-four"; "fifty-five"; "fifty-six"; "fifty-seven";
   "fifty-eight"; "fifty-nine"; "sixty"; "sixty-one"; "sixty-two";
   "sixty-three"; "sixty-four"; "sixty-five"; "sixty-six"; "sixty-seven";
   "sixty-eight"; "sixty-nine"; "seventy"; "seventy-one"; "seventy-two";
   "seventy-three"; "seventy-four"; "seventy-five"; "seventy-six";
   "seventy-seven"; "seventy-eight"; "seventy-nine"; "eighty"; "eighty-one";
   "eighty-two"; "eighty-three"; "eighty-four"; "eighty-five"; "eighty-six";
   "eighty-seven"; "eighty-eight"; "eighty-nine"; "ninety"; "ninety-one";
   "ninety-two"; "ninety-three"; "ninety-four"; "ninety-five"; "ninety-six";
   "ninety-seven"; "ninety-eight"; "ninety-nine"; "one hundred"]
> [100..121]
- |> List.map(numeral_string)
- ;;
val it : string list =
  ["one hundred"; "one hundred and one"; "one hundred and two";
   "one hundred and three"; "one hundred and four"; "one hundred and five";
   "one hundred and six"; "one hundred and seven"; "one hundred and eight";
   "one hundred and nine"; "one hundred and ten"; "one hundred and eleven";
   "one hundred and twelve"; "one hundred and thirteen";
   "one hundred and fourteen"; "one hundred and fifteen";
   "one hundred and sixteen"; "one hundred and seventeen";
   "one hundred and eighteen"; "one hundred and nineteen";
   "one hundred and twenty"; "one hundred and twenty-one"]
> [190..221]
- |> List.map(numeral_string)
- ;;
val it : string list =
  ["one hundred and ninety"; "one hundred and ninety-one";
   "one hundred and ninety-two"; "one hundred and ninety-three";
   "one hundred and ninety-four"; "one hundred and ninety-five";
   "one hundred and ninety-six"; "one hundred and ninety-seven";
   "one hundred and ninety-eight"; "one hundred and ninety-nine";
   "two hundred"; "two hundred and one"; "two hundred and two";
   "two hundred and three"; "two hundred and four"; "two hundred and five";
   "two hundred and six"; "two hundred and seven"; "two hundred and eight";
   "two hundred and nine"; "two hundred and ten"; "two hundred and eleven";
   "two hundred and twelve"; "two hundred and thirteen";
   "two hundred and fourteen"; "two hundred and fifteen";
   "two hundred and sixteen"; "two hundred and seventeen";
   "two hundred and eighteen"; "two hundred and nineteen";
   "two hundred and twenty"; "two hundred and twenty-one"]
> [990..1000]
- |> List.map(numeral_string)
- ;;
val it : string list =
  ["nine hundred and ninety"; "nine hundred and ninety-one";
   "nine hundred and ninety-two"; "nine hundred and ninety-three";
   "nine hundred and ninety-four"; "nine hundred and ninety-five";
   "nine hundred and ninety-six"; "nine hundred and ninety-seven";
   "nine hundred and ninety-eight"; "nine hundred and ninety-nine";
   "one thousand"]
> [100..120]
- |> List.map(numeral_string)
- |> List.map(fun s -> s.Replace(" ", ""))
- |> List.map(fun s -> s.Replace("-", ""))
- ;;
val it : string list =
  ["onehundred"; "onehundredandone"; "onehundredandtwo"; "onehundredandthree";
   "onehundredandfour"; "onehundredandfive"; "onehundredandsix";
   "onehundredandseven"; "onehundredandeight"; "onehundredandnine";
   "onehundredandten"; "onehundredandeleven"; "onehundredandtwelve";
   "onehundredandthirteen"; "onehundredandfourteen"; "onehundredandfifteen";
   "onehundredandsixteen"; "onehundredandseventeen"; "onehundredandeighteen";
   "onehundredandnineteen"; "onehundredandtwenty"]
> [1..5]
- |> List.map(numeral_string)
- |> List.map(fun s -> s.Replace(" ", ""))
- |> List.map(fun s -> s.Replace("-", ""))
- |> List.map(fun s -> s.Length)
- |> List.sum
- ;;
val it : int = 19
> [1..1000]
- |> List.map(numeral_string)
- |> List.map(fun s -> s.Replace(" ", ""))
- |> List.map(fun s -> s.Replace("-", ""))
- |> List.map(fun s -> s.Length)
- |> List.sum
- ;;
val it : int = 21124