8

I use DrRacket. I have problem with this code:

          (define (qweqwe n) (
                      (cond 
                        [(< n 10) #t]
                        [(>= (lastnum n) (pochtilastnum n)) (qweqwe (quotient n 10))]
                        [else #f]
                        )
                      )
    )
    (define ( RTY file1 file2 )

     (define out (open-output-file file2 #:mode  'text #:exists 'replace))  
    (define in (open-input-file file1)) 
    (define (printtofile q) (begin
                   (write q out)
                   (display '#\newline out)
                   ))
       (define (next) 
          (define n (read in)) 
(cond 
      [(equal? n eof) #t]
      [else (begin
      ((if (qweqwe n) (printtofile n) #f))
      ) (next)]
      )
)
    (next)   
   (close-input-port in)
   (close-output-port out)) 

But when I start ( RTY "in.txt" "out.txt" ) I have an error at ((if (qweqwe n) (printtofile n) #f)) :

    application: not a procedure;
    expected a procedure that can be applied to arguments
    given: #f
    arguments...: [none]

What's the problem?

ADD: I changedmy code to:

(cond 
      [(equal? n eof) #t]
      [else
      (if (qweqwe n) (printtofile n) #f)
      (next)]
      )

But the problem remains.

michaeluskov
  • 1,729
  • 7
  • 27
  • 53
  • 3
    Friend, get some code formatting skills or use 'untabify' in your editor before posting code. – GoZoner May 08 '13 at 15:44

3 Answers3

15

There are some unnecessary parenthesis, don't do this:

((if (qweqwe n) (printtofile n) #f))

Try this instead:

(if (qweqwe n) (printtofile n) #f)

Also in here:

(define (qweqwe n)
  ((cond [(< n 10) #t]
         [(>= (lastnum n) (pochtilastnum n)) (qweqwe (quotient n 10))]
         [else #f])))

It should be:

(define (qweqwe n)
  (cond [(< n 10) #t]
        [(>= (lastnum n) (pochtilastnum n)) (qweqwe (quotient n 10))]
        [else #f]))

In both cases the problem was that if you surround with () an expression, it means that you're trying to invoke a procedure. And given that the result of the if and cond expressions above don't return a procedure, an error occurs. Also, bothbegins in your original code are unnecessary, a cond has an implicit begin after each condition, same thing for the body of a procedure definition.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

You have a double set of parentheses:

((if (qweqwe n) (printtofile n) #f))

This means that Scheme first evaluates this:

(if (qweqwe n) (printtofile n) #f)

Then it expects this to evaluate to a function, call it g, which it evaluates like this:

(g)

Since your conditional form does not return a function, you probably meant to just use a single set of parentheses.

svk
  • 5,854
  • 17
  • 22
-1

Before considering the correctness of an algorithm, one must get the code to be syntactically correct - that is, it must compile. One of the beautiful aspect of Scheme programming is that the interactive environment allows one to easily compile and evaluate a program.

Your code either won't compile or won't run because you have a number of syntactic errors. Here is your syntactically correct (based on my guess as to the desired behavior) code. In part I arrive at syntactic correctness by rigorously formatting the code:

(define (qweqwe n) 
  (cond 
   [(< n 10) #t]
   [(>= (lastnum n) (pochtilastnum n)) (qweqwe (quotient n 10))]
   [else #f]))

(define (RTY file1 file2 )
  (define out (open-output-file file2 #:mode  'text #:exists 'replace))  
  (define in  (open-input-file  file1)) 
  (define (printtofile q)
    (write q out)
    (display '#\newline out))

  (define (next) 
    (define n (read in)) 
    (cond 
     [(equal? n eof) #t]
     [else
      (if (qweqwe n) (printtofile n) #f)
      (next)]))
  (next)   
  (close-input-port in)
  (close-output-port out))
GoZoner
  • 67,920
  • 20
  • 95
  • 145