2013-05-08 29 views
3

我使用DrRacket。我有这个代码的问题:计划的“预计可应用于参数的程序”

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

但是当我开始(RTY “in.txt” “out.txt”)我有((如果(qweqwe N)(printtofile N)#f的错误) ):

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

什么问题?

地址:我changedmy代码:

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

但问题仍然存在。

+2

朋友,得到一些代码格式化技能或张贴代码之前使用“untabify”在编辑器中。 – GoZoner 2013-05-08 15:44:07

+0

@ user23791查看我更新的答案 – 2013-05-08 20:31:42

回答

6

有一些不必要的括号,不这样做:

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

试试这个:

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

也正是在这里:

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

它应该是:

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

在这两种情况下,问题是如果用()包围表达式,这意味着您正在尝试调用过程。并且,假设上述ifcond表达式的结果不返回过程,则会发生错误。此外,在您的原始代码中的两个begin都是不必要的,cond在每个条件之后都有一个隐含的begin,对于过程定义的主体也是如此。

1

你有双组括号:

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

这意味着,计划首先计算这样的:

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

然后,它预计这一评估的功能,称之为克,它评估如下:

(g) 

由于您的条件表单不返回函数,所以您的可能性只想使用一组括号。

0

在考虑一个算法的正确性,一个必须得到的代码是语法正确的 - 也就是说,它必须编译。 Scheme编程的一个优点是交互式环境允许用户轻松编译和评估一个程序。

您的代码不会编译或不会运行,因为您有一些语法错误。这里是你的句法正确(根据我对所需行为的猜测)代码。在第一部分我到达语法的正确性通过严格的格式化代码:

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