fizzbuzz.ipynb (2206B)
1 { 2 "cells": [ 3 { 4 "cell_type": "code", 5 "execution_count": 51, 6 "id": "f39de10b-caca-47ca-8510-b373729e957c", 7 "metadata": {}, 8 "outputs": [ 9 { 10 "data": { 11 "text/plain": [ 12 "val as_str : int -> string = <fun>\n" 13 ] 14 }, 15 "execution_count": 51, 16 "metadata": {}, 17 "output_type": "execute_result" 18 } 19 ], 20 "source": [ 21 "let as_str idx = \n", 22 " if idx mod 3 = 0\n", 23 " then if idx mod 5 = 0\n", 24 " then \"FizzBuzz\"\n", 25 " else \"Fizz\"\n", 26 " else if idx mod 5 = 0\n", 27 " then \"Buzz\"\n", 28 " else string_of_int idx" 29 ] 30 }, 31 { 32 "cell_type": "code", 33 "execution_count": 52, 34 "id": "8f03e107-c163-4e93-896d-2daa6a443b28", 35 "metadata": {}, 36 "outputs": [ 37 { 38 "data": { 39 "text/plain": [ 40 "val print_fizz_buzz : int -> unit = <fun>\n" 41 ] 42 }, 43 "execution_count": 52, 44 "metadata": {}, 45 "output_type": "execute_result" 46 } 47 ], 48 "source": [ 49 "let print_fizz_buzz n = for i = 1 to n do \n", 50 " print_endline (as_str i)\n", 51 "done" 52 ] 53 }, 54 { 55 "cell_type": "code", 56 "execution_count": 53, 57 "id": "d46ba613-0fb0-46b2-ba3c-3c3a7b892efd", 58 "metadata": {}, 59 "outputs": [ 60 { 61 "name": "stdout", 62 "output_type": "stream", 63 "text": [ 64 "1\n", 65 "2\n", 66 "Fizz\n", 67 "4\n", 68 "Buzz\n", 69 "Fizz\n", 70 "7\n", 71 "8\n", 72 "Fizz\n", 73 "Buzz\n", 74 "11\n", 75 "Fizz\n", 76 "13\n", 77 "14\n", 78 "FizzBuzz\n" 79 ] 80 }, 81 { 82 "data": { 83 "text/plain": [ 84 "- : unit = ()\n" 85 ] 86 }, 87 "execution_count": 53, 88 "metadata": {}, 89 "output_type": "execute_result" 90 } 91 ], 92 "source": [ 93 "print_fizz_buzz 15" 94 ] 95 } 96 ], 97 "metadata": { 98 "kernelspec": { 99 "display_name": "OCaml default", 100 "language": "OCaml", 101 "name": "ocaml-jupyter" 102 }, 103 "language_info": { 104 "codemirror_mode": "text/x-ocaml", 105 "file_extension": ".ml", 106 "mimetype": "text/x-ocaml", 107 "name": "OCaml", 108 "nbconverter_exporter": null, 109 "pygments_lexer": "OCaml", 110 "version": "4.14.2" 111 } 112 }, 113 "nbformat": 4, 114 "nbformat_minor": 5 115 }