# agbell's solution

## to Grains in the Haskell Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

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. Two grains on the next. Four on the third, and so on.

There are 64 squares on a chessboard.

Write code that shows:

• how many grains were on each square, and
• the total number of grains

## 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.

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?

## Getting Started

For installation and learning resources, refer to the exercism help page.

## Running the tests

To run the test suite, execute the following command:

``````stack test
``````

#### If you get an error message like this...

``````No .cabal file found in directory
``````

You are probably running an old stack version and need to upgrade it.

#### Otherwise, if you get an error message like this...

``````No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
``````

Just do as it says and it will download and install the correct compiler version:

``````stack setup
``````

## Running GHCi

If you want to play with your solution in GHCi, just run the command:

``````stack ghci
``````

## Feedback, Issues, Pull Requests

The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

## Source

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

## Submitting Incomplete Solutions

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

### Tests.hs

``````{-# OPTIONS_GHC -fno-warn-type-defaults #-}

import Data.Foldable     (for_)
import Test.Hspec        (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Grains (square, total)

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = do
describe "square" \$ for_ squareCases squareTest
describe "total"  \$ totalTest totalCase
where

squareTest (description, n, expected) = it description assertion
where
assertion  = expression `shouldBe` expected
expression = fmap fromIntegral . square . fromIntegral \$ n

totalTest (description, expected) = it description assertion
where
assertion = fromIntegral total `shouldBe` expected

squareCases :: [(String, Integer, Maybe Integer)]
squareCases =
[ ("square 1"             ,  1, Just                   1)
, ("square 2"             ,  2, Just                   2)
, ("square 3"             ,  3, Just                   4)
, ("square 4"             ,  4, Just                   8)
, ("square 16"            , 16, Just               32768)
, ("square 32"            , 32, Just          2147483648)
, ("square 64"            , 64, Just 9223372036854775808)
, ("square negative"      , -1, Nothing                 )
, ("square 0"             ,  0, Nothing                 )
, ("square bigger than 64", 65, Nothing                 ) ]

totalCase :: (String, Integer)
totalCase = ("total grains", 18446744073709551615)``````
``````module Grains (square, total) where

square :: Int -> Integer
square x = 2 ^ (x-1)

total :: Integer
total = 2 ^ 64 -1``````