Avatar of burma-shave

burma-shave's solution

to Anagram in the LFE Track

Published at Jul 13 2018 · 4 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")
    (anagram:find "Orchestra" '("cashregister" "carthorse" "radishes"))))

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

(deftest does-not-detect-a-word-as-its-own-anagram
  (is-equal
    '("cron")
    (anagram:find "corn" '("corn" "dark" "Corn" "rank" "CORN" "cron" "park"))))
(defmodule anagram
  (export (find 2)))

(defun find (word poss-anagrams)
  (let ([sorted-word (lists:sort (string:to_lower word))])
    (lists:filter (lambda (anag)
		    (and 
		     (== sorted-word (lists:sort (string:to_lower anag)))
		     (not (== (string:to_lower word) (string:to_lower anag)))))
		  poss-anagrams)))

Community comments

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

Coming from Clojure, and might not work the way you expect. Check the LFE mailing list for a discussion of and vs andalso.

Avatar of yurrriq

You might like to (let ((... (string:to_lower anag))) ...) in your filtering function for a cheap optimization.

Avatar of burma-shave
burma-shave
Solution Author
commented over 3 years ago

Coming from Clojure, and might not work the way you expect. Check the LFE mailing list for a discussion of and vs and also.

Thanks for the tip! I like how LFE makes the short-circuiting explicit.

Avatar of yurrriq

I like how LFE makes the short-circuiting explicit.

That's good to hear. Some people don't...

This is also a common idiom: (andalso {{boolean precondition, test}} {{expression to be evaluated iff (=:= 'true test)}})

... which is roughly equivalent to Clojure's when (except test must be boolean()).

N.B. There's not when/2 or similar in LFE, when is only for guards.

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?