Created
February 2, 2023 11:41
-
-
Save schmalz/4799e83489087e4209cbcccbabf2ecb2 to your computer and use it in GitHub Desktop.
Clojure implementation of the Feature Comparison Mini-Exercise from Common Lisp: A Gentle Introduction to Symbolic Computing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns compare-descriptions.core | |
(:require [clojure.set :only intersection])) | |
(defn- right-side | |
"Given a list containing the descriptions of two objects separated by the | |
keyword :-vs-, return the description of the object to the right of the | |
separator." | |
[descriptions] | |
(rest (drop-while #(not= % :-vs-) | |
descriptions))) | |
(defn- left-side | |
"Given a list containing the descriptions of two objects separated by the | |
keyword :-vs-, return the description of the object to the left of the | |
separator." | |
[descriptions] | |
(take-while #(not= % :-vs-) | |
descriptions)) | |
(defn count-common | |
"Given a list containing the descriptions of two objects separated by the | |
keyword :-vs-, return the count of features the two objects have in common." | |
[descriptions] | |
(count (clojure.set/intersection (set (left-side descriptions)) | |
(set (right-side descriptions))))) | |
(defn compare-features | |
"Given a list containing the descriptions of two objects separated by the | |
keyword :-vs-, return a description of the number of features the two | |
objects have in common." | |
[descriptions] | |
(str (count-common descriptions) " common features")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment