Skip to content

Instantly share code, notes, and snippets.

@heathermiller
Created June 3, 2020 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heathermiller/bba61bcf95d4c46240762bfa8696fc33 to your computer and use it in GitHub Desktop.
Save heathermiller/bba61bcf95d4c46240762bfa8696fc33 to your computer and use it in GitHub Desktop.
ResistorColors in Unison

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 has a position and a numeric value. 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 color names as input and output a two digit number, even if the input is more than two colors!

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

From the example above: brown-green should return 15 brown-green-violet should return 15 too, ignoring the third color.

use base.Nat.fromText
unique type Color =
Black
| Brown
| Red
| Orange
| Yellow
| Green
| Blue
| Violet
| Grey
| White
getVal : Color -> Text
getVal c = match c with Color.Black -> "0"
Color.Brown -> "1"
Color.Red -> "2"
Color.Orange -> "3"
Color.Yellow -> "4"
Color.Green -> "5"
Color.Blue -> "6"
Color.Violet -> "7"
Color.Grey -> "8"
Color.White -> "9"
value : Color -> Color -> Nat
value a b = match .base.Nat.fromText (getVal a ++ getVal b) with
None -> 0
Some x -> x
test> value.tests.ex1 =
check ( value Brown Black == 10)
test> value.tests.ex2 =
check ( value Blue Grey == 68)
test> value.tests.ex3 =
check ( value Yellow Violet == 47)
test> value.tests.ex4 =
check ( value Orange Orange == 33)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment