Feedback

13 views
Skip to first unread message

jim

unread,
Mar 28, 2008, 10:37:54 AM3/28/08
to Clojure
Rich,

Just wanted to give you some feedback. I've been climbing the
learning curve on Clojure for about 3 weeks. In the process I've been
shifting from 20 years of imperative/object oriented programming to a
functional paradigm.

Clojure has been a huge help in this. The functional programming
ideas coupled with the regular syntax of s-exprs is a real win. I can
see hints that the constructs for concurrency will be just as powerful
and I'm looking forward to using them.

All this started clicking in the past couple of days. I've been
developing a state-machine framework in Clojure to motivate my
learning. I had a nasty imperative version that took a state-machine,
looped through a stream of inputs and returned the final state. I
changed that to a much more elegant functional routine that takes a
state-machine and lazily evaluates a stream of inputs to produce a
stream of states. Now, the input stream is not restricted to a vector
or list. It can even be a function that generates a series of values
like sensor readings or stock prices. And the state machine happily
does it's work as the inputs become available. Hopefully, I'll be
able to post the code soon.

Anyway, just wanted to say good job on designing Clojure. I, for one,
am really enjoying learning it.

Jim

Rich Hickey

unread,
Mar 28, 2008, 11:17:18 AM3/28/08
to Clojure
Thanks. I appreciate the investment on your part, and that of everyone
who actually writes some Clojure code, to learn about and think in a
new language, with a small community, written by someone you don't
know. It really pays off in experiences like yours.

Rich

Patrick Logan

unread,
Mar 28, 2008, 11:27:59 AM3/28/08
to clo...@googlegroups.com
> All this started clicking in the past couple of days. I've been
> developing a state-machine framework in Clojure to motivate my
> learning. I had a nasty imperative version that took a state-machine,
> looped through a stream of inputs and returned the final state. I
> changed that to a much more elegant functional routine that takes a
> state-machine and lazily evaluates a stream of inputs to produce a
> stream of states. Now, the input stream is not restricted to a vector
> or list. It can even be a function that generates a series of values
> like sensor readings or stock prices. And the state machine happily
> does it's work as the inputs become available. Hopefully, I'll be
> able to post the code soon.

FYI -- Here's a really nice exposition on using scheme macros to
define state machines that might give you some ideas on furthering
yours in Clojure...

It's PDF... http://ll1.ai.mit.edu/shriram-talk.pdf

-Patrick

Rich Hickey

unread,
Mar 28, 2008, 11:54:56 AM3/28/08
to Clojure
Funny you should mention that. In the first few weeks of Clojure's
release, it was mentioned on the PLT list, where Shriram here:

http://list.cs.brown.edu/pipermail/plt-scheme/2007-November/021541.html

asked for an implementation of the core idea in the paper that
underlies the presentation to which you linked:

http://www.cs.brown.edu/~sk/Publications/Papers/Published/sk-automata-macros/paper.pdf

The core algorithm is this (in Scheme):

(define machine
(letrec ([init
(lambda (stream)
(cond
[(empty? stream) true]
[else
(case (first stream)
[(c) (more (rest stream))]
[else false])]))]
[more
(lambda (stream)
(cond
[(empty? stream) true]
[else
(case (first stream)
[(a) (more (rest stream))]
[(d) (more (rest stream))]
[(r) (end (rest stream))]
[else false])]))]
[end
(lambda (stream)
(cond
[(empty? stream) true]
[else
(case (first stream)
[else false])]))])
init))

to which I replied:

http://list.cs.brown.edu/pipermail/plt-scheme/2007-November/021623.html

Which in current Clojure would look like:

(defn machine [stream]
(let [step {[:init 'c] :more
[:more 'a] :more
[:more 'd] :more
[:more 'r] :end
[:end nil] :t}]
(loop [state :init
[f & r] stream]
(when-let next (step [state f])
(or (= next :t) (recur next r))))))


(machine '(c a d a d d r ))
-> true
(machine '(c a d a d d r r))
-> nil

Rich
Reply all
Reply to author
Forward
0 new messages