🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of epequeno

epequeno's solution

to Grains in the Elm Track

Published at Apr 19 2021 · 0 comments
Test suite

Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.

There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board, with the number of grains doubling on each successive square.

There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).

Write code that shows:

  • how many grains were on a given square, and
  • the total number of grains on the chessboard

For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

  • Optimize for speed.
  • Optimize for readability.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The code you have to write is located inside the src/ directory of the exercise. Elm automatically installs packages dependencies the first time you run the tests so we can start by running the tests from the exercise directory with:

$ elm-test

To automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the tests suite in the file tests/Tests.elm, be sure to remove the skip <| calls from each test until you get them all passing!


JavaRanch Cattle Drive, exercise 6 http://www.javaranch.com/grains.jsp

Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.


module Tests exposing (tests)

import Expect
import Grains exposing (square)
import Json.Encode exposing (Value)
import Test exposing (..)

tests : Test
tests =
    describe "Grains"
        [ describe "square"
            [ test "of 1" <|
                \() -> Expect.equal (Just 1) (square 1)
            , skip <|
                test "of 2" <|
                    \() -> Expect.equal (Just 2) (square 2)
            , skip <|
                test "of 3" <|
                    \() -> Expect.equal (Just 4) (square 3)
            , skip <|
                test "of 4" <|
                    \() -> Expect.equal (Just 8) (square 4)
            , skip <|
                test "of 16" <|
                    \() -> Expect.equal (Just 32768) (square 16)
            , skip <|
                test "of 32" <|
                    \() -> Expect.equal (Just 2147483648) (square 32)
            , skip <|
                test "square 0 raises an exception" <|
                    \() -> Expect.equal Nothing (square 0)
            , skip <|
                test "negative square raises an exception" <|
                    \() -> Expect.equal Nothing (square -1)

               Where are the bigger test values?!? Because Javascript's numbers
               can't represent values higher than `Number.MAX_SAFE_INTEGER`
               (i.e. 9007199254740991), we chose to exclude these final values
               to avoid the weirdness. A bit more information can be found
               here: https://github.com/elm-lang/elm-compiler/issues/1246
module Grains exposing (square)

square : Int -> Maybe Int
square n =
    if n < 1 then

        Just (2 ^ (n - 1))

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?