Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Functional > Re: a golden ol...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 40 Topic 2778 of 2841
Post > Topic >>

Re: a golden oldie challenge: Eliza

by Mark Tarver <dr.mtarver@[EMAIL PROTECTED] > Feb 22, 2008 at 03:59 AM

On 22 Feb, 11:49, Mark Tarver <dr.mtar...@[EMAIL PROTECTED]
> wrote:
> Having got 3936 LOC through a 4000 LOC implementation, I thought I'd
> do some recreational
> hacking and do an old old program I've not looked at for some time -
> Eliza. =A0 You all know Eliza well enough for me not to have to spell it
> out. =A0The challenge is to implement or dig up an Eliza program (you
> don't have to write it yourself) in your favourite FPL. =A0Note that the
> script that drives Eliza's responses should not be counted towards the
> LOC count. =A0Some constraints.
>
> 1. =A0The script itself should be changeable by any novice. =A0That is
to
> say that it should not
> =A0 =A0 =A0be a pile of hard-wired code written in the native language
of
> the program or require
> =A0 =A0 =A0deep programming skills.
>
> 2. =A0The program should receive keyboard input from the user -
> including the usual punctuation
> =A0 =A0 =A0and any characters he wants to enter without crashing.
>
> During the Harrop Wars on comp.lang.lisp a lot of stuff was thrown
> around about the desirability of pattern matching. =A0The challenge is
> interesting because it involves a style of pattern-matching called
> 'segment pattern matching' that is not hard-wired into most FPLs and
> I'd like to see how well different FPLs cope with something outside
> the standard.
>
> Oh last thing; don't get too uptight about this. =A0It's only a bit of
> fun.
>
> Mark

Well here is my shot at it in Qi.  Total LOC excluding script is 70
LOC. You should run it under Qi 9.2 (latest release) because the
system function 'read-chars-as-stringlist' had a bug that was patched
in that release.  My script is very boring ;).

I looked for a Haskell/ML equivalent and found zilch.  Norvig's PAIP
contains a Lisp version in two files (eliza and eliza1) that is about
150 LOC excluding comments and script.

Mark
___________________________________________________________
(set *script* [
               [[X "like" Y] ["Why" "do" "you" "like" Y "?"]]
               [[X "father" Y] ["Tell me about your father."]]
               [[X] ["That's very interesting. Do go on."]]])

(define eliza
  -> (do (output "hi~%?- ") (eliza-loop (user) (value *script*))))

(define eliza-loop
  User Script -> (let Responses (map (/. S (pmatch User S)) Script)
                      Interesting (remove-if no-match? Responses)
                      ScriptError (if (empty? Interesting)
                                      (error "script failure!")
                                      _)
                      Choice (nth (+ (random (length Interesting)) 1)
Interesting)
                      Response (respond-with Choice)
                      Output (output "~{~A ~}~%~%?- " Response)
                      (eliza-loop (user) Script)))

(define respond-with
  [[] R] -> R
  [[[X V] | B] R] -> (respond-with [B (rep X R V)]))

(define no-match?
  [#\Escape _] -> true
  _ -> false)

(define user
  -> (read-chars-as-stringlist (user-loop (read-char _)) whitespace?))

(define whitespace?
  #\Space -> true
  #\Tab -> true
  #\, -> true
  #\. -> true
  _ -> false)

(define user-loop
  #\Newline -> []
  C -> [C | (user-loop (read-char _))])

(define remove-if
  _ [] -> []
  F [X | Y] -> (if (F X) (remove-if F Y) [X | (remove-if F Y)]))

(define pmatch
  User [I R] -> [(pmatch-help I User []) R])

(define pmatch-help
   X X B -> B
   [X | Y] [X | Z] B -> (pmatch-help Y Z B)
   [X | Y] Z B <- (let NilBind (nilbind X B)
                       ValX (value-in X NilBind)
                       (pmatch-help (rep X Y ValX) Z NilBind))
                       where (variable? X)
   [X | Y] [W | Z] B <- (pmatch-help [X | Y] Z (consbind X W B))
                         where (variable?
X)
   _ _ _ -> #\Escape)

(define nilbind
  X [] -> [[X []]]
  X [[X V] | B] -> [[X V] | B]
  X [Y | Z] -> [Y | (nilbind X Z)])

(define consbind
  X W [] -> [[X [W]]]
  X W [[X V] | B] -> [[X (append V [W])] | B]
  X W [Y | B] -> [Y | (consbind X W B)])

(define rep
  _ [] _ -> []
  X [X | Y] V -> (append V (rep X Y V))
  X [Y | Z] V -> [Y | (rep X Z V)])

(define value-in
  X B -> (head (tail (assoc X B))))




 40 Posts in Topic:
a golden oldie challenge: Eliza
Mark Tarver <dr.mtarve  2008-02-22 03:49:44 
Re: a golden oldie challenge: Eliza
Mark Tarver <dr.mtarve  2008-02-22 03:59:02 
Re: a golden oldie challenge: Eliza
William James <w_a_x_m  2008-02-22 14:09:58 
Re: a golden oldie challenge: Eliza
"John Thingstad"  2008-02-23 13:00:30 
Re: a golden oldie challenge: Eliza
"Stevan Apter"   2008-02-23 13:21:00 
Re: a golden oldie challenge: Eliza
Ken Tilton <kennytilto  2008-02-23 15:24:42 
Re: a golden oldie challenge: Eliza
"Stevan Apter"   2008-02-23 16:42:31 
Re: a golden oldie challenge: Eliza
Ken Tilton <kennytilto  2008-02-23 20:55:56 
Re: a golden oldie challenge: Eliza
"Stevan Apter"   2008-02-24 07:44:32 
Re: a golden oldie challenge: Eliza
Mark Tarver <dr.mtarve  2008-02-22 17:12:49 
Re: a golden oldie challenge: Eliza
William James <w_a_x_m  2008-02-22 19:19:31 
Re: a golden oldie challenge: Eliza
Szabolcs <szhorvat@[EM  2008-02-23 14:12:17 
Re: a golden oldie challenge: Eliza
Szabolcs <szhorvat@[EM  2008-02-23 14:26:03 
Re: a golden oldie challenge: Eliza
Ben Bacarisse <ben.use  2008-02-24 00:01:08 
Re: a golden oldie challenge: Eliza
William James <w_a_x_m  2008-02-23 20:14:36 
Re: a golden oldie challenge: Eliza
Szabolcs <szhorvat@[EM  2008-02-24 01:51:00 
Re: a golden oldie challenge: Eliza
Mark Tarver <dr.mtarve  2008-02-24 03:59:56 
Re: a golden oldie challenge: Eliza
Rainer Joswig <joswig@  2008-02-24 18:29:12 
Re: a golden oldie challenge: Eliza
Neelakantan Krishnaswami   2008-02-24 18:03:20 
Re: a golden oldie challenge: Eliza
Mark Tarver <dr.mtarve  2008-02-24 04:06:09 
Re: a golden oldie challenge: Eliza
William James <w_a_x_m  2008-02-24 05:30:38 
Re: a golden oldie challenge: Eliza
"Stevan Apter"   2008-02-24 08:59:51 
Re: a golden oldie challenge: Eliza
William James <w_a_x_m  2008-02-24 06:22:31 
Re: a golden oldie challenge: Eliza
"Stevan Apter"   2008-02-24 09:37:17 
Re: a golden oldie challenge: Eliza
William James <w_a_x_m  2008-02-24 06:42:47 
Re: a golden oldie challenge: Eliza
"Stevan Apter"   2008-02-24 10:56:28 
Re: a golden oldie challenge: Eliza
William James <w_a_x_m  2008-02-24 06:57:22 
Re: a golden oldie challenge: Eliza
Dimitre Liotev <notmy@  2008-02-24 10:00:29 
Re: a golden oldie challenge: Eliza
"Stevan Apter"   2008-02-24 10:49:22 
Re: a golden oldie challenge: Eliza
Arved Sandstrom <asand  2008-02-24 17:28:25 
Re: a golden oldie challenge: Eliza
"Stevan Apter"   2008-02-24 13:03:11 
Re: a golden oldie challenge: Eliza
Ken Tilton <kennytilto  2008-02-24 13:59:55 
Re: a golden oldie challenge: Eliza
Dimitre Liotev <notmy@  2008-02-24 11:18:27 
Re: a golden oldie challenge: Eliza
William James <w_a_x_m  2008-02-24 12:36:12 
Re: a golden oldie challenge: Eliza
William James <w_a_x_m  2008-02-24 13:11:47 
Re: a golden oldie challenge: Eliza
William James <w_a_x_m  2008-02-24 13:26:38 
Re: a golden oldie challenge: Eliza
Arved Sandstrom <asand  2008-02-25 17:16:43 
Re: a golden oldie challenge: Eliza
William James <w_a_x_m  2008-02-24 13:35:20 
Re: a golden oldie challenge: Eliza
Brian Adkins <lojicdot  2008-02-25 09:52:13 
Re: a golden oldie challenge: Eliza
"Stevan Apter"   2008-02-25 16:58:13 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat May 17 0:03:13 CDT 2008.