简体   繁体   中英

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)

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>...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM