the use of regular expressions can certainly make the code smaller,
as your example demonstrates. i wonder though whether it is entirely
in keeping with the spirit of the exercise, which requires that:
1. The script itself should be changeable by any novice. That is to
say that it should not
be a pile of hard-wired code written in the native language of
the program or require
deep programming skills.
not that it would help make the k code shorter, since regexp isn't
built into k.
"William James" <w_a_x_man@[EMAIL PROTECTED]
> wrote in message
news:5fd7be4f-8465-44fd-9fed-1ac81ed3b0a0@[EMAIL PROTECTED]
> Stevan Apter wrote:
>> thanks for asking ken.
>>
>> e:{$[(#b)=i:(|/'b:(w@[EMAIL PROTECTED]
(w:(`$" "\:x@[EMAIL PROTECTED]
".?"))in`)in\:/:A)?1b;E@[EMAIL PROTECTED]
>> " "~n:c@[EMAIL PROTECTED]
(c:{x@[EMAIL PROTECTED]
i)ss/:N;c
>> .q.ssr[c;n;" "/:$(),P'(w[k],p:1_'(0,1+k:b[i]?1b)_`,w)"I"$n]]}
>>
>> it's just a single eye-watering case statement:
>>
>> if no keyword match in A, then return a default response from E
>> else if no substitution in the response c picked from C, return c
>> else substitute and return
>
> This does so little that it should be no more than 2 lines.
> In Ruby:
>
> S =
> [ /father|mother|brother|sister/i, "Tell me about your 0."],
> [ /\b(am|i'm) (.*)/i, ["Why are you 2?","Have you always been 2?"]],
> [ /\bI was (.*)/i, ["Why were you 1?","I can't believe you were
> 1."]],
> [ /\bI will (.*)/i, "Do you think it's wise to 1?"],
> [ /\bI (.*)/i, "Why do you 1?" ],
> [ /\b(you|your|yours)\b/i, ["We're talking about you, not me.",
> "Please don't be so personal."]],
> [ /.*/, ["That's very interesting. Do go on.",
> "Tell me more.",
> "I'm not sure that I understand you fully.",
> "Can you elaborate on that?" ]]
>
> (gets;sub(/[.!?,; ]+$/,"");x=Array(S.find{|a|$m=$_.match(a[0])}[1])
> puts x[rand(x.size)].gsub(/\d/){$m.to_a[$&.to_i]}) while 9
>
> Although cryptic, it should make more sense to most programmers
> than the K code.


|