LoginSignup
1
0

More than 1 year has passed since last update.

多言語FizzBuzzチャレンジ21日目:OCaml

Last updated at Posted at 2022-12-20

これまでのまとめ

本日のお品書き

なんだか一瞬Twitterで話題になったOCamlさん。

FizzBuzz

  • 関数型言語(だと思うん)だけどforもあることがわかったOCamlさん
  • いや、意地でもパターンマッチと再帰でやりくりしてみます
    • パターンマッチh::restでリストの先頭とそれ以外をデコンストラクションしている
fizzbuzz.ml
let rec fizzbuzz x =
    match x with
    | [] -> print_string ""
    | h::rest ->
        if h mod 15 = 0 then print_endline "FizzBuzz"
        else if h mod 3 = 0 then print_endline "Fizz"
        else if h mod 5 = 0 then print_endline "Buzz"
        else print_endline (string_of_int h);
        fizzbuzz(rest);;

(* Erlangのlists:seq(1,100)相当 ↓ *)
fizzbuzz (list.init 100 (fun x -> x+1));;

おまけ:実行方法

docker run --rm --mount type=bind,source=$(pwd),target=/app -w /app ocaml/opam ocaml fizzbuzz.ml
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0