Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

racket (lisp) error: expected a procedure that can be applied to arguments

Does anyone know why this results in a LISP error even though the calculations are carried out right?
The print_pascal routine does return a #f everywhere it can return something.

#!/usr/bin/racket
#lang racket/base

(define (pascal_value row column)
  (cond ((< row 0) 0)
        ((< column 0) 0)
        ((= column 1) 1)
        ((= column row) 1)
        ((> column row) 0)
        (else (+ (pascal_value (- row 1) (- column 1)) (pascal_value (- row 1) column )))
        )
  )

(pascal_value 6 4)
(define (pascal_triangle max_rows)
  (define (print_pascal row column)
    (cond
     ((> row max_rows) (printf "done\n") #f)
     (else (cond ((= column row) (
                                  (printf "~a\n" (pascal_value row column))
                                  (print_pascal (+ row 1) 1)
                                  ) #f)
                 (else (
                        (printf "~a " (pascal_value row column))
                        (print_pascal row (+ column 1))
                        )
                       #f
                       )
                 )
           #f
           )
     )
    #f
    )
  (print_pascal 1 1)
  )

(pascal_triangle 7)
[I] âžś ./1_12.lisp
10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
done
application: not a procedure;
 expected a procedure that can be applied to arguments
  given: #<void>
  context...:
   /home/ubuntu/Software/code_samples/sicp_exercises/1_12.lisp:17:2: print_pascal
   [repeats 28 more times]
   body of "/home/ubuntu/Software/code_samples/sicp_exercises/1_12.lisp"

Edit: the following changes worked based on accepted answer

(define (pascal_triangle max_rows)
  (define (print_pascal row column)
    (cond
     ((> row max_rows) (printf "done\n"))
     (else (cond ((= column row) (printf "~a\n" (pascal_value row column)) (print_pascal (+ row 1) 1))
                 (else (printf "~a " (pascal_value row column)) (print_pascal row (+ column 1)))))
     ))
  (print_pascal 1 1)
  )

(pascal_triangle 9)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem is this line:

     ((> row max_rows) ((printf "done\n") #f))

When you write ((printf "done\n") #f) it means that (printf "done\n") should return a function, which will be called with the argument #f. printf returns #<void>, which is not a function.

If you want to execute multiple expressions in the consequence of a cond clause, they should not be put into a nested list. That should be

     ((> row max_rows)
      (printf "done\n")
      #f)

In places that don’t have a body with multiple expressions like this, such as if, you use (begin <expr> <expr> ...)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading