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
error i am not able to correct
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.
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.)
-
- Posts: 4
- Joined: Thu Oct 10, 2013 6:45 am
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..
Code: Select all
(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
Currently I'm planning a Scheme compiler :p