What do you mean by "similar functionality"? Without using mutable
state, you won't be able to retain the defnode-style interface. But
obviously a twenty-questions program like this is easy to implement in
a purely functional way:

(defn ask [questions node]
  (let [data (questions node)]
    (assert data)
    (if (string? data)
      (println "The answer is" data)
      (let [[question yes-node no-node] data]
        (println question)
        (print ">> ")
        (flush)
        (recur questions (if (= (read) 'yes) yes-node no-node))))))

(def sample-questions
     {:people ["Is the person a man?" :male :female]
      :male ["Is he living?" :liveman :deadman]
      :deadman ["Was he American?" :us :them]
      :us ["Is he on a coin?" :coin :cidence]
      :coin ["Is the coin a penny?" :penny :coins]
      :penny "Abraham Lincoln"})

(ask sample-questions :people)

-Per

On Tue, Apr 13, 2010 at 11:29 PM, Aravindh Johendran
<ajohend...@gmail.com> wrote:
> Is there a way to write this in clojure without java's hashmaps or
> clojure's atom/ref/var-binding/etc? The program doesn't have to be an
> exact translation. I just want similar functionality.
>
> ----------------------------------------------------------------------------------------------
> From chapter 6 of On Lisp - Figure 6.5
>
> (defvar *nodes* (make-hash-table))
>
> (defun defnode (name conts &optional yes no)
>  (setf (gethash name *nodes*)
>        (if yes
>            #’(lambda ()
>                (format t "~A~%>> " conts)
>                (case (read)
>                  (yes (funcall (gethash yes *nodes*)))
>                  (t (funcall (gethash no *nodes*)))))
>              #’(lambda () conts))))
>
> (defnode ’people "Is the person a man?" ’male ’female)
> (defnode ’male "Is he living?" ’liveman ’deadman)
> (defnode ’deadman "Was he American?" ’us ’them)
> (defnode ’us "Is he on a coin?" ’coin ’cidence)
> (defnode ’coin "Is the coin a penny?" ’penny ’coins)
> (defnode ’penny ’lincoln)
>
> (funcall (gethash ’people *nodes*))
> -----------------------------------------------------------------------------------------
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>
> To unsubscribe, reply using "remove me" as the subject.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to