Skip to content

Instantly share code, notes, and snippets.

@schmalz
Created February 2, 2023 11:41
Show Gist options
  • Save schmalz/4799e83489087e4209cbcccbabf2ecb2 to your computer and use it in GitHub Desktop.
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
(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