Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

application: not a procedure racket

i am new to racket. i am trying create a list from the input of the user and when the value 0 is entred the first three elements are printed.

here is the code:

#lang racket
(define lst '())
(define (add)
   (define n(read))
   (if (= n 0)
      ;then
      (
        list (car lst) (cadr lst) (caddr lst) 
       )
      ;else
      (
        (set! lst (append lst (list n)))
        (add)
       )
     )
 )
(add)

i tested the program with the values 1 2 3 4 5 0

but i keep getting this error:

application: not a procedure;
 expected a procedure that can be applied to arguments
  given: #<void>
  arguments...:
   '(1 2 3)

can anyone help me figure out what's wrong.

like image 261
Tarounen Avatar asked Jun 06 '26 16:06

Tarounen


1 Answers

If you have more than one expression in the "then" or "else" parts, you must enclose them inside a begin, because a pair of () in Scheme are used for function application - that explains the error you're getting. Try this:

(define (add)
   (define n (read))
   (if (= n 0)
      ; then
      (list (car lst) (cadr lst) (caddr lst))
      ; else
      (begin
        (set! lst (append lst (list n)))
        (add))))
like image 98
Óscar López Avatar answered Jun 10 '26 09:06

Óscar López



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!