error i am not able to correct

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
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.

error i am not able to correct

Postby jeltedeproft » Thu Oct 10, 2013 6:57 am

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
jeltedeproft
 
Posts: 4
Joined: Thu Oct 10, 2013 6:45 am

Re: error i am not able to correct

Postby nuntius » Thu Oct 10, 2013 8:52 pm

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.)
User avatar
nuntius
 
Posts: 500
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: error i am not able to correct

Postby jeltedeproft » Fri Oct 11, 2013 3:12 am

thanks perfect answer
jeltedeproft
 
Posts: 4
Joined: Thu Oct 10, 2013 6:45 am

Re: error i am not able to correct

Postby sylwester » Sat Oct 12, 2013 12:22 pm

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
sylwester
 
Posts: 84
Joined: Mon Jul 11, 2011 2:53 pm


Return to Homework

Who is online

Users browsing this forum: No registered users and 0 guests

cron