scheme basic loop

Posted by utku on Stack Overflow See other posts from Stack Overflow or by utku
Published on 2011-01-01T17:29:38Z Indexed on 2011/01/01 17:54 UTC
Read the original article Hit count: 210

Filed under:
|
|

I'm trying to write a scheme func that behaves in a way similar to a loop.

(loop min max func)

This loop should perform the func between the range min and max (integers)

-- one of an example like this

(loop 3 6 (lambda (x) (display (* x x)) (newline)))

9
16
25
36

and I define the function as

( define ( loop min max fn)
    (cond
        ((>= max min) ( ( fn min ) ( loop (+ min 1 ) max fn)  ) )
    )
)

when I run the code I get the result then an error occur. I couldn't handle this error.

(loop 3 6 (lambda (x) (display(* x x))(newline)))

9
16
25
36

Backtrace: In standard input: 41: 0* [loop 3 6 #]

In utku1.scheme: 9: 1 (cond ((>= max min) ((fn min) (loop # max fn))))

10: 2 [# ...

10: 3* [loop 4 6 #]

9: 4 (cond ((>= max min) ((fn min) (loop # max fn))))

10: 5 [# ...

10: 6* [loop 5 6 #]

9: 7 (cond ((>= max min) ((fn min) (loop # max fn))))

10: 8 [# ...

10: 9* [loop 6 6 #]

9: 10 (cond ((>= max min) ((fn min) (loop # max fn))))

10: 11 [# #]

utku1.scheme:10:31: In expression ((fn min) (loop # max ...)): utku1.scheme:10:31: Wrong type to apply: #<unspecified> ABORT: (misc-error)

© Stack Overflow or respective owner

Related posts about function

Related posts about loops