Range (was: overloading in function) 
Author Message
 Range (was: overloading in function)

Thanks for all the help on writing a Range function. Here are full
implementation of Range in Mathematica, in Perl, and in Scheme.

Btw, they are not representative of best coding in each language, and I have
not taken the pains to varify that they all behave exactly alike under all
possible inputs. Nor are they coded to show as much analogy as possible.
(but they all use recursion) They are here purely for fun.

------------------------------------------------
(*Mathematica code*)

range::"usage"= "range[a,b,dx] returns a list from a to b with increment dx.
dx can be negative if a > b. range[n] is equivalent to range[1,n,1].
range[a,b] is equivalent to range[a,b,1].";


range[a_]:=range[1,a,1];
range[a_,b_]:=range[a,b,1];




=
{
    a};

  Prepend[range[a+dx,b,dx],a];

------------------------------------------------

# perl code

sub range {



Quote:
};

sub rangeInternal {

if ($dx == 0) {print "increment cannot be zero."; return ()} elsif
($a1 == $b1) {return $a1;} elsif
( ((($b1 - $a1) > 0) && ($dx < 0)) || ((($b1 - $a1) < 0) && ($dx > 0)) ) {
print "bad argument order."; return ();};

if ((($a1 < $b1) && ($b1 < ($a1 + $dx))) ||
        (($a1 > $b1) && ($b1 > ($a1 + $dx))))
{$a1;} else
{ ($a1, &rangeInternal ($a1 + $dx, $b1, $dx) );};

Quote:
};

------------------------------------------------
;and here are two versions of Range in Scheme


; Date: 27 Dec 1997 11:32:38 -0600

(define range
  (lambda args
    (let ((arg-count (length args)))
      (if (not (<= 1 arg-count 3))
          (error 'range "wrong number of arguments"))
      (let ((start (if (= arg-count 1) 1 (car args)))
            (finish (if (= arg-count 1) (car args) (cadr args)))
            (increment (if (= arg-count 3) (caddr args) 1)))
        (let ((beyond (cond ((positive? increment) >)
                            ((negative? increment) <)
                            (else (error 'range "zero increment")))))
          (let loop ((result '())
                     (counter start))
            (if (beyond counter finish)
                (reverse result)
                (loop (cons counter result) (+ counter increment)))))))))


;Date: 27 Dec 1997 17:31:50 +0100

(define (range a . x)
  (if (null? x) (make-range 1 a 1)
      (let ((b (car x)) (dx (cdr x)))
        (if (null? dx) (make-range a b 1)
            (make-range a b (car dx))))))

(define (make-range a b dx)
  (if (= 0 dx) (breakpoint "0 delta in make-range")
      (if (= (sign (- b a)) (sign dx))
          (let ((end-range
                 (if (> dx 0) (lambda (x) (> x b))
                     (lambda (x) (< x b)))))
            (letrec ((really-make-range
                      (lambda (x res)
                        (if (end-range x) (reverse res)
                            (really-make-range (+ x dx) (cons x res))))))
              (really-make-range a '() )))
          '() )))
(define (sign x) (if (>= x 0) 1 -1))

------------------------------------------------


  http://www.*-*-*.com/ ~xah/Wallpaper_dir/c0_WallPaper.html
 "(intelligence (lisp (pigs (perl (unix baggage (C (brain tumor)))))))"



Sat, 17 Jun 2000 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Can standard Perl functions be overloaded ?

2. PERL OO: Function overloading

3. Overloading builtin functions

4. A question of overloading functions

5. overloading in function

6. Q: overloading gmtime and localtime functions: Guru Opinion Sought

7. Overloading 'print' function

8. function overloading in Perl5

9. range of return values for rand function

10. use overload or %OVERLOAD ???

11. overload method package and unload overloads

12. cgi.pm cookie function - am I missing something?

 

 
Powered by phpBB® Forum Software