2013-05-08 30 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))