Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of rootulp

rootulp's solution

to Anagram in the Elm Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".

Elm Installation

Refer to the Exercism help page for Elm installation and learning resources.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed.

$ elm-package install --yes

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!

Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (..)

import Anagram exposing (detect)
import Expect
import Test exposing (..)


tests : Test
tests =
    describe "Anagram"
        [ test "no matches" <|
            \() ->
                Expect.equal []
                    (detect "diaper" [ "hello", "world", "zombies", "pants" ])
        , skip <|
            test "detects simple anagram" <|
                \() ->
                    Expect.equal [ "tan" ]
                        (detect "ant" [ "tan", "stand", "at" ])
        , skip <|
            test "does not detect false positives" <|
                \() ->
                    Expect.equal []
                        (detect "galea" [ "eagle" ])
        , skip <|
            test "detects multiple anagrams" <|
                \() ->
                    Expect.equal [ "stream", "maters" ]
                        (detect "master" [ "stream", "pigeon", "maters" ])
        , skip <|
            test "does not detect anagram subsets" <|
                \() ->
                    Expect.equal []
                        (detect "good" [ "dog", "goody" ])
        , skip <|
            test "detects anagram" <|
                \() ->
                    Expect.equal [ "inlets" ]
                        (detect "listen" [ "enlists", "google", "inlets", "banana" ])
        , skip <|
            test "detects even more anagrams" <|
                \() ->
                    Expect.equal [ "gallery", "regally", "largely" ]
                        (detect "allergy" [ "gallery", "ballerina", "regally", "clergy", "largely", "leading" ])
        , skip <|
            test "does not detect indentical words" <|
                \() ->
                    Expect.equal [ "cron" ]
                        (detect "corn" [ "corn", "dark", "Corn", "rank", "CORN", "cron", "park" ])
        , skip <|
            test "does not detect non-anagrams with identical checksum" <|
                \() ->
                    Expect.equal []
                        (detect "mass" [ "last" ])
        , skip <|
            test "detects anagrams case-insensitively" <|
                \() ->
                    Expect.equal [ "Carthorse" ]
                        (detect "Orchestra" [ "cashregister", "Carthorse", "radishes" ])
        , skip <|
            test "detects anagrams using case-insensitive subject" <|
                \() ->
                    Expect.equal [ "carthorse" ]
                        (detect "Orchestra" [ "cashregister", "carthorse", "radishes" ])
        , skip <|
            test "detects anagrams using case-insensitve possible matches" <|
                \() ->
                    Expect.equal [ "Carthorse" ]
                        (detect "orchestra" [ "cashregister", "Carthorse", "radishes" ])
        , skip <|
            test "does not detect a word as its own anagram" <|
                \() ->
                    Expect.equal []
                        (detect "banana" [ "Banana" ])
        , skip <|
            test "does not detect a anagram if the original word is repeated" <|
                \() ->
                    Expect.equal []
                        (detect "go" [ "go Go GO" ])
        , skip <|
            test "anagrams must use all letters exactly once (go)" <|
                \() ->
                    Expect.equal []
                        (detect "tapper" [ "patter" ])
        , skip <|
            test "eliminates anagrams with the same checksum" <|
                \() ->
                    Expect.equal []
                        (detect "mass" [ "last" ])
        , skip <|
            test "detects unicode anagrams" <|
                \() ->
                    Expect.equal [ "ΒΓΑ", "γβα" ]
                        (detect "ΑΒΓ" [ "ΒΓΑ", "ΒΓΔ", "γβα" ])
        , skip <|
            test "eliminates misleading unicode anagrams" <|
                \() ->
                    Expect.equal []
                        (detect "ΑΒΓ" [ "ABΓ" ])
        , skip <|
            test "capital word is not own anagram" <|
                \() ->
                    Expect.equal []
                        (detect "BANANA" [ "Banana" ])
        , skip <|
            test "anagrams must use all letters exactly once (banana)" <|
                \() ->
                    Expect.equal []
                        (detect "patter" [ "tapper" ])
        ]

elm-package.json

{
    "version": "3.0.0",
    "summary": "Exercism problems in Elm.",
    "repository": "https://github.com/exercism/elm.git",
    "license": "BSD3",
    "source-directories": [
        ".",
        ".."
    ],
    "exposed-modules": [],
    "dependencies": {
        "elm-lang/core": "5.0.0 <= v < 6.0.0",
        "elm-community/elm-test": "4.0.0 <= v < 5.0.0"
    },
    "elm-version": "0.18.0 <= v < 0.19.0"
}
module Anagram exposing (..)

detect: String -> List String -> List String
detect word possibleAnagrams =
    List.filter (findAnagrams word) possibleAnagrams

findAnagrams: String -> (String -> Bool)
findAnagrams word =
    (\ possibleAnagram -> isAnagramCaseInsensitive word possibleAnagram)

isAnagramCaseInsensitive: String -> String -> Bool
isAnagramCaseInsensitive word possibleAnagram =
    isAnagram (String.toLower word) (String.toLower possibleAnagram)

isAnagram: String -> String -> Bool
isAnagram word possibleAnagram =
    word /= possibleAnagram &&
    sortedCharacters  word == sortedCharacters possibleAnagram

sortedCharacters: String -> List Char
sortedCharacters word =
    String.toList word
        |> List.sort

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?