Page 1 of 1

error i am not able to correct

Posted: Thu Oct 10, 2013 6:57 am
by jeltedeproft
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

Posted: Thu Oct 10, 2013 8:52 pm
by nuntius
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

Posted: Fri Oct 11, 2013 3:12 am
by jeltedeproft
thanks perfect answer

Re: error i am not able to correct

Posted: Sat Oct 12, 2013 12:22 pm
by sylwester
Or you can use a character instead of a symbol..

Code: Select all

(display-n #\c 5) ; prints "ccccc", returns anything