Avatar of stephl001

stephl001's solution

to Atbash Cipher in the Elm Track

Published at Sep 25 2019 · 0 comments
Test suite

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards. The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba

It is a very weak cipher because it only has one possible key, and it is a simple monoalphabetic substitution cipher. However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, and punctuation is excluded. This is to make it harder to guess things based on word boundaries.


  • Encoding test gives gvhg
  • Decoding gvhg gives test
  • Decoding gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt gives thequickbrownfoxjumpsoverthelazydog

Elm Installation

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

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!


Wikipedia http://en.wikipedia.org/wiki/Atbash

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 AtbashCipher exposing (decode, encode)
import Expect
import Test exposing (..)

tests : Test
tests =
    describe "AtbashCipher"
        [ test "encode no" <|
            \() -> Expect.equal "ml" (encode "no")
        , skip <|
            test "encode yes" <|
                \() -> Expect.equal "bvh" (encode "yes")
        , skip <|
            test "encode OMG" <|
                \() -> Expect.equal "lnt" (encode "OMG")
        , skip <|
            test "encode O M G" <|
                \() -> Expect.equal "lnt" (encode "O M G")
        , skip <|
            test "encode long word" <|
                \() -> Expect.equal "nrmwy oldrm tob" (encode "mindblowingly")
        , skip <|
            test "encode numbers" <|
                \() -> Expect.equal "gvhgr mt123 gvhgr mt" (encode "Testing, 1 2 3, testing.")
        , skip <|
            test "encode sentence" <|
                \() -> Expect.equal "gifgs rhurx grlm" (encode "Truth is fiction.")
        , skip <|
            test "encode all things" <|
                \() ->
                    Expect.equal "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
                        (encode "The quick brown fox jumps over the lazy dog.")
        , skip <|
            test "decode word" <|
                \() -> Expect.equal "exercism" (decode "vcvix rhn")
        , skip <|
            test "decode sentence" <|
                \() ->
                    Expect.equal "anobstacleisoftenasteppingstone"
                        (decode "zmlyh gzxov rhlug vmzhg vkkrm thglm v")
module AtbashCipher exposing (decode, encode)

identityMapping : List Char
identityMapping =
    "abcdefghijklmnopqrstuvwxyz" |> String.toList

reverseMapping : List Char
reverseMapping =
    identityMapping |> List.reverse

type CypherChar
    = Encoded Char
    | Unencoded Char

type alias Mapper =
    CypherChar -> CypherChar

cypherToChar : CypherChar -> Char
cypherToChar cc =
    case cc of
        Encoded c ->

        Unencoded c ->

charToCypher : Char -> CypherChar
charToCypher =
    Char.toLower >> Unencoded

mapIf : Char -> Char -> Mapper
mapIf fromLetter toLetter mapLetter =
    case mapLetter of
        Encoded _ ->

        Unencoded c ->
            if c == fromLetter then
                Encoded toLetter


createMapper : List Char -> List Char -> CypherChar -> CypherChar
createMapper m1 m2 =
    List.map2 mapIf identityMapping reverseMapping
        |> List.foldl (>>) identity

forwardMapper : CypherChar -> CypherChar
forwardMapper =
    createMapper identityMapping reverseMapping

reverseMapper : CypherChar -> CypherChar
reverseMapper =
    createMapper reverseMapping identityMapping

chunkString : Int -> String -> List String
chunkString chunkSize str =
    case str of
        "" ->

        _ ->
            String.slice 0 chunkSize str :: (chunkString chunkSize <| String.dropLeft chunkSize str)

encodeWithMapper : Mapper -> String -> String
encodeWithMapper mapper =
        >> List.filter Char.isAlphaNum
        >> List.map (charToCypher >> mapper >> cypherToChar)
        >> String.fromList

encode : String -> String
encode =
    encodeWithMapper forwardMapper
        >> chunkString 5
        >> String.join " "

decode : String -> String
decode =
    encodeWithMapper reverseMapper

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?