This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

error i am not able to correct

4 posts · 5055 views

Hello everyone, i am in my first year of computer programming and we are working with drracket, scheme for now.

One of the tasks was to create a program to could display a certain character an "n" amount of times.

as such :

>(display-n c 5)

ccccc


here is my code :

(define display-n
(lambda (c n)
(display c)
(if (> n 1)
(display-n c (- n 1)))))

and i got it to work with numbers, but when i do it with characters, i get the error : cannot refrence an identifier before its definition. I thought that when you used lambda, te parameters arent evaluated. So i dont understand why scheme rejects any non-number.

thanks for your help

Re: error i am not able to correct

The parameters are evaluated when you type them in, before they are passed to the lambda form. Use a quote to protect the character. (Or a macro if quotes aren't allowed.)

Re: error i am not able to correct

thanks perfect answer

Re: error i am not able to correct

Or you can use a character instead of a symbol..
(display-n #\c 5) ; prints "ccccc", returns anything
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p