ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

yurrriq's solution

to Anagram in the LFE Track

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

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"`.

For installation and learning resources, refer to the exercism help page.

To run the provided tests, you will need `make`. Open a terminal window and run the following from the exercise directory:

``````make test
``````

You should now be able to see the results of the test suite for the exercise.

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.

anagram-tests.lfe

``````(defmodule anagram-tests
(behaviour ltest-unit)
(export all))

(include-lib "ltest/include/ltest-macros.lfe")

(deftest no-matches
(is-equal () (anagram:find "diaper" '("hello" "world" "zombies" "pants"))))

(deftest detect-simple-anagram
(is-equal '("tan") (anagram:find "ant" '("tan" "stand" "at"))))

(deftest does-not-confuse-different-duplicates
(is-equal () (anagram:find "galea" '("eagle"))))

(deftest eliminate-angram-subsets
(is-equal () (anagram:find "good" '("dog" "goody"))))

(deftest detect-anagram
(is-equal
'("inlets")
(anagram:find "listen" '("enlists" "google" "inlets" "banana"))))

(deftest multiple-anagrams
(is-equal
'("gallery" "regally" "largely")
(anagram:find
"allergy"
'("gallery" "ballerina" "regally" "clergy" "largely" "leading"))))

(deftest case-insensitive-subject
(is-equal
'("carthorse")

(deftest case-insensitive-candidate
(is-equal
'("Carthorse")

(deftest does-not-detect-a-word-as-its-own-anagram
(is-equal
'("cron")
(anagram:find "corn" '("corn" "dark" "Corn" "rank" "CORN" "cron" "park"))))``````
``````;;; ============================================================ [ anagram.lfe ]

(defmodule anagram
(doc "Find anagrams for a word in a list of candidates.")
(export (find 2))
(import (from lists (filter 2) (sort 1))
(rename string ((to_lower 1) lower-case))))

;;; ==================================================================== [ API ]

(defun find (word candidates)
"Return the sublist of `candidates` which are anagrams of `word`."
(filter (anagram? (normalize word)) candidates))

;;; ===================================================== [ Internal functions ]

(defun anagram? (normalized-word)
"Return a unary function that takes a `candidate`, normalizes it and
returns true iff it is an anagram of `normalized-word`.

(lambda (candidate)
(anagram? (normalize candidate) normalized-word)))

(defun anagram?
"Given a normalized candidate and a normalized word,
return true iff they are anagrams.

([`#(,la ,sa) `#(,lb ,sb)]
(andalso (=/= la lb) (=:= sa sb))))

(defun normalize (word)
"Return a tuple of the form `#(lower-cased-word sorted-lower-cased-word).`"
(let ((lowered (lower-case word)))
(tuple lowered (sort lowered))))

;;; ==================================================================== [ EOF ]``````