0

I'm trying to print a list of pairs of values (representing key/value pairs) in Racket.

Here's the code I have right now:

#lang racket

(define (write-json keyvalues)
  (displayln "{")
  (for-each
    (lambda (kv) (
      (displayln (format "~a: ~a," (car kv) (cdr kv))))) 
    keyvalues)
  (displayln "}"))

(write-json (list (cons "a" 1) (cons "b" 2)))

When I run the example, it prints:

{
a: 1,

Then, it crashes with this error message:

application: not a procedure;
 expected a procedure that can be applied to arguments
  given: #<void>
  arguments...: [none]
  context...:

  /.../racket/collects/racket/private/map.rkt:58:19: loop
   "test.rkt": [running body]
   for-loop
   run-module-instance!125
   perform-require!78

Any idea what's going on?

Thanks!

Juiblex
  • 3
  • 3
  • 2
    as an aside: DrRacket has a JSON package that can certainly print out JSON for you. see https://pkgs.racket-lang.org – John Clements Mar 23 '18 at 15:46
  • Possible duplicate of [My code signals the error "application: not a procedure" or "call to non procedure"](https://stackoverflow.com/questions/48064955/my-code-signals-the-error-application-not-a-procedure-or-call-to-non-procedu) – Sylwester Mar 23 '18 at 21:09

1 Answers1

2

This is a paren issue. You have an extra set of parentheses around your lambda's body, ie:

( (displayln (format "~a: ~a;" (car kv) (cdr kv))) )

Since displayln is used for side effect, its output is void, hence why your error message states that you're trying to run (#<void>).

In general, whenever you get an error stating "expected a procedure that can be applied to arguments", see if you have parentheses issues in your code block. Editors like Dr. Racket would highlight that region for you.

assefamaru
  • 2,751
  • 2
  • 10
  • 14