Skip navigation

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

http://groups.google.com/group/ai-renaissance/files

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))

Clojure:
(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
b
user=> ({:a 1 :b 2 :c 3} :b) ; a map applied to key :b
2
user=> (#{'a 'b 'c} 'a) ; A set applied to an element
a
user=> (#{'a 'b 'c} 'x) ; the same set applied to a non-element
nil

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: