Boolean Autocrat
Boolean Autocrat

Reputation: 367

Call a function from a If conditional

I am trying to call range-check in an if conditional inside a function called add-check.
You can find range-check below:

;;; range-check takes 4 arguments:  row, numrows, col, numcols It checks if
;;;  0 <= row < numrows and similarly for col and numcols.  If both row and col are in 
;;;  range, range-check returns #t.  If either are out of range, rangecheck  returns #f
(define (range-check row numrows col numcols)
  (not (or (< row 0) (< col 0) (>= row numrows) (>= col numcols))))

Func is function of rows and columns. Numrows and numcols are a number. I am trying to call range check under the function add-check below.


This is add check:

(define (add-check func numrows numcols)
   (if((range-check? (car func) numrows (cdr func) numcols)))
     (cons '((car (func)) (cdr (func))))
     #\.)

When I run this I get the error:

if: bad syntax in: (if ((range-check? (car func) numrows (cdr func) numcols)))

which I'm assuming implies an error in the if condition.

I'm positive I'm making a silly syntax issue or multiple syntax issues. I'm new to scheme and this IS for a school project. So please help to whatever capacity you can.

Upvotes: 0

Views: 148

Answers (2)

simmone
simmone

Reputation: 379

args is not clear, I guess what you want , this is my version:

#lang racket

(define (range-check? row numrows col numcols)
  (not (or (< row 0) (< col 0) (>= row numrows) (>= col numcols))))

(define (some-func row col)
  (format "row:~a col:~a" row col))

(define (add-check rowandcol numrows numcols num-func)
   (if (range-check? (car rowandcol) numrows (cdr rowandcol) numcols)
     (num-func (car rowandcol) (cdr rowandcol))
     #\.))

(add-check '(1 . 2) 10 10 some-func)

(add-check '(1 . 2) 1 1 some-func)

(add-check '(1 . 2) 2 3 (lambda (num col) #t))

Upvotes: 1

Sorawee Porncharoenwase
Sorawee Porncharoenwase

Reputation: 6502

I think you want:

(define (range-check? row numrows col numcols)
  (not (or (< row 0) (< col 0) (>= row numrows) (>= col numcols))))

(define (add-check func numrows numcols)
  (if (range-check? (car (func)) numrows (cdr (func)) numcols)
      (cons (car (func)) (cdr (func)))
      #\.))
  1. I guess that you forgot to call func in (range-check? (car func)...).
  2. You forgot ? in range-check.
  3. You have extra function call around (range-check....) which is definitely wrong since range-check? returns a boolean which is not callable.
  4. if should have then and else branches inside its parentheses,
  5. (cons '((car (func)) (cdr (func)))) is wrong. cons consumes two arguments, but you provide one, which is a quoted expression. I assume that they should be (car (func)) and (cdr (func)).
  6. If possible, use DrRacket. You can select all and indent, which will reveal some of these mistakes easily.
  7. Another way to write range-check? more concisely: (and (< -1 row numrows) (< -1 col numcols)).
  8. You might want to consider calling func just once and bind its result to a variable, and use that variable instead. In case func is expensive, doing so will make your code more efficient.
  9. You can simplify (cons (car (func)) (cdr (func))) to just (func)!

Upvotes: 2

Related Questions