Skip navigation

I am posting my latest source code in both Clojure and PLT Scheme:

Both implementations are purely functional. They turned out to be pretty short overall. There are a few interesting Clojure idioms:

PLT Scheme:
(remove ‘b ‘(a b c d))

(remove #{’b} ‘(a b c d))

The notation #{’b} is a set literal with a single symbol ‘b as its element. It turns out that in Clojure, arrays, structures, and sets all act as functions on keys:

user=> (['a 'b 'c] 1) ; array of three elements applied to an index
user=> ({:a 1 :b 2 :c 3} :b) ; a map applied to key :b
user=> (#{'a 'b 'c} 'a) ; A set applied to an element
user=> (#{'a 'b 'c} 'x) ; the same set applied to a non-element

A more accurate translation into scheme would be:

(filter (lambda (item) (not (equal? item 'b))) '(a b c d))

In Clojure the set can be used as a function to identify its members. The remove function uses a predicate function to filter the elements.

One Comment

  1. The idiom becomes more fun when you have multiple items in the set:

    (remove ‘#{b d} ‘(a b c d))
    -> (a c)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: