2013-10-08 22 views
1
#lang scheme 

(define consecutive? 
    (lambda(a b c) 
    ((cond [(and (= (- b a) 1) (or (= (- c b) 1) (= (- a c) 1))) "true"] 
      [(and (= (- a b) 1) (or (= (- c a) 1) (= (- b c) 1))) "true"] 
      [(and (= (- c a) 1) (or (= (- a b) 1) (= (- b c) 1))) "true"] 
      [(and (= (- a c) 1) (or (= (- c b) 1) (= (- b a) 1))) "true"] 
      [else "false"])))) 

(consecutive? 2 3 4) 

爲什麼這是給出錯誤?「應用程序:不是程序」,同時檢查連續的數字

+1

這不是你錯誤的來源,但是:考慮使用'#t'和'#f'來代替字符串,並且在你的'cond'子句中考慮這個重複。 – Inaimathi

+1

[Racket PLAI Application not a Procedure]的可能重複(http://stackoverflow.com/questions/19022704/racket-plai-application-not-a-procedure)。除此之外,如果您使用scheme標籤搜索[「not a procedure」(http://stackoverflow.com/search?q=+%22not+a+procedure%22+%5Bscheme%5D))在已經回答的地方會發現很多問題。 '((cond ...))'期望'(cond ...)'產生一個結果'r',這是一個過程,然後嘗試調用'r'。 –

回答

5

你有一個雙((條件之前,它應該是

(define consecutive? 
    (lambda(a b c) 
    (cond 
     [(and (= (- b a) 1)(or (= (- c b) 1)(= (- a c) 1))) "true"] 
     [(and (= (- a b) 1)(or (= (- c a) 1)(= (- b c) 1))) "true"] 
     [(and (= (- c a) 1)(or (= (- a b) 1)(= (- b c) 1))) "true"] 
     [(and (= (- a c) 1)(or (= (- c b) 1)(= (- b a) 1))) "true"] 
     [else "false"]))) 

編輯如果我正確理解你的算法,一個更普遍的版本是:

(define (diff1? lst) 
    (or (empty? lst) 
     (empty? (cdr lst)) 
     (and (= 1 (- (cadr lst) (car lst))) 
      (diff1? (cdr lst))))) 

(define (consecutive-new? . lst) 
    (diff1? (sort lst <))) 

其中DIFF1 ?只是檢查數字是連續的(n,n + 1,n + 2 ...)和連續新的?與列表排序