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

NobbZ's solution

to Anagram in the LFE Track

Published at Jul 13 2018 · 3 comments
Test suite

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.


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.


(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
    (anagram:find "listen" '("enlists" "google" "inlets" "banana"))))

(deftest multiple-anagrams
    '("gallery" "regally" "largely")
      '("gallery" "ballerina" "regally" "clergy" "largely" "leading"))))

(deftest case-insensitive-subject
    (anagram:find "Orchestra" '("cashregister" "carthorse" "radishes"))))

(deftest case-insensitive-candidate
    (anagram:find "Orchestra" '("cashregister" "Carthorse" "radishes"))))

(deftest does-not-detect-a-word-as-its-own-anagram
    (anagram:find "corn" '("corn" "dark" "Corn" "rank" "CORN" "cron" "park"))))
;; -*- mode: lisp -*-

(defmodule anagram
  (export (find 2)))

(defun find (original wordlist)
  (let* ((lower-original (string:to_lower original))
         (sorted-lower-original (lists:sort lower-original)))
    (lists:filter (lambda (word)
                    (let* ((lower-word (string:to_lower word))
                           (sorted-lower-word (lists:sort lower-word)))
                      (and (== sorted-lower-word sorted-lower-original)
                           (/= lower-word lower-original))))

Community comments

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

I'd love to see another iteration with the same separation of responsibilities as your other submissions :)

Avatar of yurrriq

Also, and does not short circuit while andalso does, so you could rewrite your filter function to be lazier.

Avatar of NobbZ

You are right, I really need to learn to choose more carefully anong lazy/strict and/or versionsā€¦ That's a problem I have in erlang as well (but I tend to overuse shortcutting versions there)

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?