Page 1 of 1

Simple Printing/formatting a list in Scheme

PostPosted: Thu Mar 31, 2016 3:58 pm
by leo77
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)

Re: Simple Printing/formatting a list in Scheme

PostPosted: Sat Apr 02, 2016 4:01 pm
by nuntius
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))