Avatar of agbell

agbell's solution

to Anagram in the Haskell Track

Published at Jul 13 2018 · 1 comment
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".

Hints

To complete this exercise you need to implement the function anagramsFor, that takes a word and a group of words, returning the ones that are anagrams of the given word.

If it is your first time solving this exercise, it is recommended that you stick to the provided signature:

anagramsFor :: String -> [String] -> [String]

Later, it may be a good idea to revisit this problem and play with other data types and libraries:

  • Text, from package text.
  • Sequence and Set, from package containers.
  • MultiSet, from package multiset

The test suite was intentionally designed to accept almost any type signature that makes sense, so you are encouraged to find the one you think is the best.

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

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

{-# LANGUAGE RecordWildCards #-}

import Data.Foldable     (for_)
import GHC.Exts          (fromList, toList)
import Test.Hspec        (Spec, describe, it, shouldMatchList)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Anagram (anagramsFor)

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

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

    test Case{..} = it description $ expression `shouldMatchList` expected
      where
        expression = map toList
                   . toList
                   . anagramsFor (fromList subject)
                   . fromList
                   . map fromList
                   $ candidates

data Case = Case { description ::  String
                 , subject     ::  String
                 , candidates  :: [String]
                 , expected    :: [String]
                 }

cases :: [Case]
cases = [ Case { description = "no matches"
               , subject     = "diaper"
               , candidates  = [ "hello", "world", "zombies", "pants"]
               , expected    = []
               }
        , Case { description = "detects two anagrams"
               , subject     = "master"
               , candidates  = ["stream", "pigeon", "maters"]
               , expected    = ["stream", "maters"]
               }
        , Case { description = "does not detect anagram subsets"
               , subject     = "good"
               , candidates  = ["dog", "goody"]
               , expected    = []
               }
        , Case { description = "detects anagram"
               , subject     = "listen"
               , candidates  = ["enlists", "google", "inlets", "banana"]
               , expected    = ["inlets"]
               }
        , Case { description = "detects three anagrams"
               , subject     = "allergy"
               , candidates  = ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]
               , expected    = ["gallery", "regally", "largely"]
               }
        , Case { description = "does not detect non-anagrams with identical checksum"
               , subject     = "mass"
               , candidates  = ["last"]
               , expected    = []
               }
        , Case { description = "detects anagrams case-insensitively"
               , subject     = "Orchestra"
               , candidates  = ["cashregister", "Carthorse", "radishes"]
               , expected    = ["Carthorse"]
               }
        , Case { description = "detects anagrams using case-insensitive subject"
               , subject     = "Orchestra"
               , candidates  = ["cashregister", "carthorse", "radishes"]
               , expected    = ["carthorse"]
               }
        , Case { description = "detects anagrams using case-insensitive possible matches"
               , subject     = "orchestra"
               , candidates  = ["cashregister", "Carthorse", "radishes"]
               , expected    = ["Carthorse"]
               }
        , Case { description = "does not detect a anagram if the original word is repeated"
               , subject     = "go"
               , candidates  = ["go Go GO"]
               , expected    = []
               }
        , Case { description = "anagrams must use all letters exactly once"
               , subject     = "tapper"
               , candidates  = ["patter"]
               , expected    = []
               }
        , Case { description = "capital word is not own anagram"
               , subject     = "BANANA"
               , candidates  = ["Banana"]
               , expected    = []
               }
        ]
module Anagram (anagramsFor) where

import Data.List
import Data.Char

anagramsFor :: String -> [String] -> [String]
anagramsFor a = filter isAnagram
  where
    isAnagram b1 = isMatch aKey (key b1)
    isMatch (k1, v1) (k2,v2) = k1 == k2 && v1 /= v2
    aKey = key a
    key x = let x' = map toLower x in (sort x', x')

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of agbell

This is the same as iteration #3 except that I have factored out aKey. I think that should make key a only called once per anagramsFor call.

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?