hawkeye
hawkeye

Reputation: 35692

Scheme equivalent of print function in Clojure

I'm looking at Scheme (Dr-Scheme) coming from Clojure.

In Clojure I can type

(print 'a 'b 'c)

and the print function figures out that this is an arbitrary number of non-string arguments and prints them out separated by a space.

In Scheme the print function expects a single argument.

Is there a way to get the equivalent of Clojure's print function in Scheme?

Upvotes: 2

Views: 1129

Answers (3)

Aslan986
Aslan986

Reputation: 10324

I use this set of definitions to print multiple arguments separated by new line:

(define (println x) (display x) (newline))
(define (printlist l) (begin
                        (println (car l))
                        (if (not (null? (cdr l))) (printlist (cdr l)))))  
(define (multiprint  . args) (begin
                                (if (not (null? args)) (printlist args) 
                                    (println "Error: multiprint requires at least one argument"))))

Upvotes: 0

soegaard
soegaard

Reputation: 31147

Perhaps you are looking for trace ?

#lang racket

(define (foo x y)
  (+ x y))

(define (bar x)
  (+ (foo 1 x)
     (foo 2 (+ x 1))))

(require racket/trace)
(trace foo)

And then in the interaction window:

> (bar 3)

>(foo 1 3)
<4
>(foo 2 4)
<6
10

Upvotes: 2

John Clements
John Clements

Reputation: 17203

Interesting... you can roll one of those pretty easily, but I'm not sure I see the need for it. For instance:

#lang racket

(define (print-many . args)
  (display
   (apply
    string-append
    (add-between (map print-to-string args) " "))))

(define (print-to-string arg) (format "~v" arg))

(print-many 3 4 5 'b '(3 3 4))

In general, though, I'm thinking that if you're generating output for a user, you're going to want better control over the output, and if you're not generating output for a user, you're just as happy slapping a pair of parens around it and making it into a list.

What's the use case for this?

Upvotes: 2

Related Questions