Here's your original code:

#!/usr/bin/perl use strict; use warnings; my $name; my $upto; my @response = ("That's interesting.","Eh, boring...","Ugh.","Cool! K +eep up the good work!"); sub april { print "Hi, my name is April! What's yours? "; chomp($name = <STDIN>); print "Hi ", $name, ", nice to meet you! What are you up to? "; for (;;) { chomp($upto = <STDIN>); $upto eq "nothing" and last; print $response[rand @response], "\n"; print "Anything else? "; } print "Goodbye, $name!\n"; } april();

And here's how I rewrote it:

#!/usr/bin/perl use strict; use warnings; my @response = ( "That's interesting.", "Eh, boring...", "Ugh.", "Cool! Keep up the good work!" ); chat('April', @response); sub chat { my ($my_name, @responses) = @_; print "Hi, my name is $my_name! What's yours? "; chomp(my $name = <STDIN>); print "Hi $name, nice to meet you! What are you up to? "; my $response = ''; until ($response eq 'nothing') { chomp($response = <STDIN>); print $responses[rand @responses], "\n"; print "Anything else? "; } print "Goodbye, $name!\n"; }

matra555: That was a fun little snippet you posted. In response to your query, I went ahead and made a few quick changes to reflect something closer to how I would write this.

I was glad to see you using strict and warnings. Failure to use those are two of the biggest mistakes new Perl programmers make. Your overall code was reasonable and easy to read and you generally had well chosen names. This is good and bodes well for your future in Perl :)


OK, I know this place is about Perl, but I suspect a few people here are familiar with Scheme. Can anyone tell me why I get the following problem with mit-scheme?

    1 ]=> (load "test.scm")

    ;Loading "test.scm" -- done
    ;Value: sqrt

    1 ]=> (sqrt 4)

    ;Aborting!: maximum recursion depth exceeded

And test.scm looks like this:

(define (new-if predicate then-clause else-clause) (cond (predicate then-clause) (else else-clause))) (define (sqrt x) (define (sqrt-iter guess x) (new-if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define (improve guess x) (average guess (/ x guess))) (define (average x y) (/ (+ x y) 2)) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (sqrt-iter 1.0 x))

That works find if the sqrt definition has new-if changed to if. If you're curious, I got the problem from Section 1.1.7 of the Wizard book.