Examples ======== Calculating Pi with Monte Carlo Method -------------------------------------- Pseudo-random number generator with :py:class:`~hymn.types.state.State` monad: .. literalinclude:: ../examples/monte_carlo.hy :language: clojure :lines: 7- Example output: .. code-block:: sh $ ./monte_carlo.hy 50000 the estimate for pi = 3.14232 Calculating Sum --------------- Wicked sum function with :py:class:`~hymn.types.writer.Writer` monad: .. literalinclude:: ../examples/sum.hy :language: clojure :lines: 7- Example output: .. code-block:: sh $ ./sum.hy 123 456 789 sum: 1368 Interactive Greeting -------------------- Greeting from :py:class:`~hymn.types.continuation.Continuation` monad: .. literalinclude:: ../examples/greeting.hy :language: clojure :lines: 7- Example output: .. code-block:: sh $ ./greeting.hy Hi, what is your name? Please tell me your name! $ ./greeting.hy Hi, what is your name? Marvin Welcome, Marvin! Greatest Common Divisor ----------------------- Logging with :py:class:`~hymn.types.writer.Writer` monad: .. literalinclude:: ../examples/gcd.hy :language: clojure :lines: 7- Example output: .. code-block:: sh $ ./gcd.hy 24680 1352 calculating the greatest common divisor of 24680 and 1352 24680 mod 1352 = 344 1352 mod 344 = 320 344 mod 320 = 24 320 mod 24 = 8 24 mod 8 = 0 the result is: 8 Project Euler Problem 9 ----------------------- Solving problem 9 with :py:class:`~hymn.types.list.List` monad .. literalinclude:: ../examples/euler9.hy :language: clojure :lines: 7- Example output: .. code-block:: sh $ ./euler9.hy Project Euler Problem 9 - list monad example https://projecteuler.net/problem=9 There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. 31875000 Project Euler Problem 29 ------------------------ Solving problem 29 with :py:func:`~hymn.operations.lift` and :py:class:`~hymn.types.list.List` monad .. literalinclude:: ../examples/euler29.hy :language: clojure :lines: 7- Example output: .. code-block:: sh $ ./euler29.hy Project Euler Problem 29 - lift and list monad example https://projecteuler.net/problem=29 How many distinct terms are in the sequence generated by a to the power of b for 2 <= a <= 100 and 2 <= b <= 100? 9183 Solving 24 Game --------------- Nondeterministic computation with :py:class:`~hymn.types.list.List` monad and error handling with :py:class:`~hymn.types.maybe.Maybe` monad: .. literalinclude:: ../examples/solve24.hy :language: clojure :lines: 7- Example output: .. code-block:: sh $ ./solve24.hy 2 3 8 8 ((2 * 8) - 8) * 3 (3 / 2) * (8 + 8) 3 / (2 / (8 + 8)) ((8 - 2) - 3) * 8 ((8 * 2) - 8) * 3 ((8 - 3) - 2) * 8 8 * (8 - (2 + 3)) ((8 + 8) / 2) * 3 (8 + 8) / (2 / 3) (8 + 8) * (3 / 2) 8 * (8 - (3 + 2)) ((8 + 8) * 3) / 2