[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/icuEmmu2    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Mar 18:
; loopy loops

(define (range . args)
  (case (length args)
    ((1) (range 0 (car args) (if (negative? (car args)) -1 1)))
    ((2) (range (car args) (cadr args) (if (< (car args) (cadr args)) 1 -1)))
    ((3) (let ((le? (if (negative? (caddr args)) >= <=)))
           (let loop ((x(car args)) (xs '()))
             (if (le? (cadr args) x)
                 (reverse xs)
                 (loop (+ x (caddr args)) (cons x xs))))))
    (else (error 'range "unrecognized arguments"))))

(define (f n) (range 1 (+ n 1)))

(display (f 10)) (newline) (display "---") (newline)

(define (f n)
  (for-each (lambda (x) (display x) (newline)) (range 1 (+ n 1))))

(f 10) (display "---") (newline)

(define (f n)
  (and (< 1 n) (f (- n 1)))
    (display n) (newline))

(f 10) (display "---") (newline)

(define (f n)
  (let f ((n n) (i 1))
    (display i) (newline)
    (and (< 1 n) (f (- n 1) (+ i 1)))))

(display (f 10)) (newline) (display "---") (newline)

(define (void) (if #f #f))

(define (f n)
  (define (g i)
    (display i) (newline)
    (and (< i n) (g (+ i 1))))
  (g 1) (void))

(f 10) (display "---") (newline)

(define (f n)
  (define (g n i)
    (let ((j (/ n)))
      (display i) (newline)
      (g (- n 1) (+ i 1))))
    (g n 1))

(f 10)


Output:
(1 2 3 4 5 6 7 8 9 10)
---
1
2
3
4
5
6
7
8
9
10
---
1
2
3
4
5
6
7
8
9
10
---
1
2
3
4
5
6
7
8
9
10
#f
---
1
2
3
4
5
6
7
8
9
10
---
1
2
3
4
5
6
7
8
9
10
/: division by zero

 === context ===
Line 47:2: g


Create a new paste based on this one


Comments: