Reputation: 367
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
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
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)))
#\.))
func
in (range-check? (car func)...)
.?
in range-check
.(range-check....)
which is definitely wrong since range-check?
returns a boolean which is not callable. if
should have then and else branches inside its parentheses,(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))
.range-check?
more concisely: (and (< -1 row numrows) (< -1 col numcols))
.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.(cons (car (func)) (cdr (func)))
to just (func)
!Upvotes: 2