Simple Printing/formatting a list in Scheme

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.

Simple Printing/formatting a list in Scheme

Postby leo77 » Thu Mar 31, 2016 3:58 pm

Hi everyone,

I just want to print out a list (containing a string and a number) in this format:

Orange : 3

With my code, I'm getting the following error:

Orange : (3). . application: not a procedure;
expected a procedure that can be applied to arguments
given: #<void>
arguments...:

Would appreciate the guidance.

Thanks!

Code: Select all

#lang scheme

(define t '( "Orange" 3))


(define print-object (lambda (m)
                     (
                      (display (name m))
                      (display " : ")
                      (display (priority m))
                      )))

(print-object t)
leo77
 
Posts: 1
Joined: Thu Mar 31, 2016 3:50 pm

Re: Simple Printing/formatting a list in Scheme

Postby nuntius » Sat Apr 02, 2016 4:01 pm

Hi,

In Scheme, the first expression in a list is treated as a function to be evaluated.
You have an extra set of parentheses in the lambda form.
This is causing the undesired evaluation.

In languages like C/C++/Java, an extra level of {}s does nothing.
In the Lisp family of languages, an extra level of ()s usually causes evaluation.

Try the following code.

Code: Select all
(define print-object
  (lambda (m)
    (display (car m))
    (display " : ")
    (display (cadr m))))
(print-object '("Orange" 3))
User avatar
nuntius
 
Posts: 530
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA


Return to Homework

Who is online

Users browsing this forum: No registered users and 0 guests

cron