Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@
"strings"
]
},
{
"slug": "resistor-colors",
"uuid": "61be7bcd-42fb-44b7-a51d-1d081173f876",
"core": false,
"unlocked_by": "pangram",
"difficulty": 2,
"topics": [
"strings"
]
},
{
"slug": "accumulate",
"uuid": "5c5a9256-a55e-4bda-baf3-96d052732e11",
Expand Down
82 changes: 82 additions & 0 deletions exercises/resistor-colors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Resistor Colors

If you want to build something using a Raspberry Pi, you'll probably use _resistors_. For this exercise, you need to know two things about them:

* Each resistor has a resistance value.
* Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values. Each band acts as a digit of a number. For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.

In this exercise, you are going to create a helpful program so that you don't have to remember the values of the bands. The program will take two colors as input, and output the correct number.

The band colors are encoded as follows:

- Black: 0
- Brown: 1
- Red: 2
- Orange: 3
- Yellow: 4
- Green: 5
- Blue: 6
- Violet: 7
- Grey: 8
- White: 9


## Getting Started

For installation and learning resources, refer to the
[exercism help page](http://exercism.io/languages/haskell).

## Running the tests

To run the test suite, execute the following command:

```bash
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:

```bash
stack setup
```

## Running *GHCi*

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

```bash
stack ghci
```

## Feedback, Issues, Pull Requests

The [exercism/haskell](https://github.com/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

Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1464](https://github.com/exercism/problem-specifications/issues/1464)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
16 changes: 16 additions & 0 deletions exercises/resistor-colors/examples/success-standard/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: resistor-colors

dependencies:
- base

library:
exposed-modules: ResistorColors
source-dirs: src

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- resistor-colors
- hspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module ResistorColors (Color(..), value) where

data Color =
Black
| Brown
| Red
| Orange
| Yellow
| Green
| Blue
| Violet
| Grey
| White
deriving (Eq, Show, Read)

convert :: Color -> Int
convert Black = 0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice that it would be possible to use http://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#t:Enum here.

I have not yet determined whether it would be a good idea to do so.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same thought.

What you don't get with deriving Enum is an explicit annotation that Black is 0, etc.

Making an explicit instance Enum where ... has the drawback that you need to specify both toEnum and fromEnum, even though the exercise only needs the one. There are ways to avoid that, but the first way (using a dynamic lookup table) should be discouraged, and the second way (using Data and Typeable) is probably a little too complicated to serve as a good example.

So I'm content with having a simple convert function.

convert Brown = 1
convert Red = 2
convert Orange = 3
convert Yellow = 4
convert Green = 5
convert Blue = 6
convert Violet = 7
convert Grey = 8
convert White = 9

value :: [Color] -> Int
value = read . concatMap (show . convert)
21 changes: 21 additions & 0 deletions exercises/resistor-colors/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: resistor-colors
version: 1.0.0.0

dependencies:
- base

library:
exposed-modules: ResistorColors
source-dirs: src
ghc-options: -Wall
# dependencies:
# - foo # List here the packages you
# - bar # want to use in your solution.

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- resistor-colors
- hspec
17 changes: 17 additions & 0 deletions exercises/resistor-colors/src/ResistorColors.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module ResistorColors (Color(..), value) where

data Color =
Black
| Brown
| Red
| Orange
| Yellow
| Green
| Blue
| Violet
| Grey
| White
deriving (Eq, Show, Read)

value :: [Color] -> Int
value cs = error "You need to implement this function."
1 change: 1 addition & 0 deletions exercises/resistor-colors/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resolver: lts-12.4
44 changes: 44 additions & 0 deletions exercises/resistor-colors/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# LANGUAGE RecordWildCards #-}

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

import ResistorColors (Color(..), value)

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

specs :: Spec
specs = describe "value" $ for_ cases test
where

test Case{..} = it explanation assertion
where
explanation = unwords [show input, "-", description]
assertion = value input `shouldBe` expected

data Case = Case { description :: String
, input :: [Color]
, expected :: Int
}

cases :: [Case]
cases = [ Case { description = "Brown and black"
, input = [Brown, Black]
, expected = 10
}
, Case { description = "Blue and grey"
, input = [Blue, Grey]
, expected = 68
}
, Case { description = "Yellow and violet"
, input = [Yellow, Violet]
, expected = 47
}
, Case { description = "Orange and orange"
, input = [Orange, Orange]
, expected = 33
}
]