On Feb 24, 8:37 am, "Stevan Apter" <encryptednos...@[EMAIL PROTECTED]
> wrote:
> 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.
I understand your misgivings, and I considered that myself.
Regular expressions are used in many programming languages and
Unix utilities (awk, Perl, grep, sed, etc.). All good text
editors support them, so anyone who is serious about using
computers should familiarize himself with them. For example,
say he's editing a large file and wants to find a line that
has "foo" followed later in the line by "bar"; he'd simply
search for "foo.*bar". How would that be done without
regular expressions? I don't think that learning about
regular expressions means learning "deep programming skills".
But yes, using my shorter program would be harder for
a novice.
>
> not that it would help make the k code shorter, since regexp isn't
> built into k."William James" <w_a_x_...@[EMAIL PROTECTED]
> wrote in
messagenews: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.


|