Question

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.

Was it helpful?

Solution

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

OTHER TIPS

I had a similar problem, in a function i called a parameter with the same name of a structure, so, trying to create an instance of that structure i got the same error.

example:

> (struct example (param1 param2) #:transparent)
> (define e (example 1 2))
> e
(example 1 2)
> (define (fn e)
    (example (example-param1 e) 0))
> (fn e)
(example 1 0)
> (define (fn example)
    (example (example-param1 example) 0))
> (fn e)
application: not a procedure;
 expected a procedure that can be applied to arguments
  given: (example 1 2)
  arguments...: 

I hope this helps

Your code have a few problems, for example it will fail if you enter less than 3 elements. Also, it is not considered good style to define variables at the module level.

I'd suggest the following:

(define (add)
  (define (sub cnt lst)
    (define n (read))
    (if (= n 0)
        (reverse lst)
        (if (< cnt 3)
            (sub (add1 cnt) (cons n lst))
            (sub cnt lst))))
  (sub 0 '()))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top