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)

Why this is giving an error?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Hari Chaudhary
  • 630
  • 1
  • 7
  • 20
  • 1
    This isn't the source of your error, but: consider using `#t` and `#f` instead of strings, and factoring out that repetition in your `cond` clauses. – Inaimathi Oct 08 '13 at 16:43
  • 1
    possible duplicate of [Racket PLAI Application not a Procedure](http://stackoverflow.com/questions/19022704/racket-plai-application-not-a-procedure). In addition to that one, if you search for ["not a procedure" with the scheme tag](http://stackoverflow.com/search?q=+%22not+a+procedure%22+%5Bscheme%5D) you'll find lots of questions where this is already answered. `((cond …))` expects `(cond …)` to produce a result `r` that is a procedure, and then tries to call `r`. – Joshua Taylor Oct 08 '13 at 17:36

1 Answers1

5

You have a double (( before cond. it should be

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

EDIT If I understand your algorithm correctly, a more general version would be:

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

where diff1? just checks that numbers are consecutive (n, n+1, n+2 ...) and consecutive-new? calls the former with the list sorted.

uselpa
  • 18,732
  • 2
  • 34
  • 52