Scheme How to check 2 conditions in one paragraph?

I need to create a subfunction that will return to me all the adjacent nodes that I need for this question in the Scheme. I am new to the circuit, I don’t know how I can combine two conditions into one test case?

Basically, my algorithm should check if the node is on the edge or not. In this case, I use a 5x5 grid.

If both nodes are on the corner, then both are either 1 or 5, which will have only 2 neighboring nodes. If only one of the nodes hit the edge, I will have a 3 node return value. If there is no edge around the node, I will have 4 node return.

My problem is how can I put 2 test cases in one point?

(define (neighbors l w)
  (if (= 1 l) and (= 1 w)
      (display l w))) --at top left corner

Here I want to evaluate whether l and w are equal to 1. Now this does not work, because I can not use "and" or "such keywords in the syntax, and I can not use and / or combine them. Any ideas? Or should i do something else?

+3
source share
2 answers

Have you tried this:

(define (neighbors l w)
  (if (and (= 1 l) (= 1 w))
     (display l w))) --at top left corner

Because when I look at this , it looks like it works

+7
source

when and if they are more convenient than if if in the conditional there is only one branch:

(define (neighbors l w)
  (when (and (= 1 l) (= 1 w))
     (display l) 
     (display #\space) 
     (display w) 
     (newline)))

Note that when a branch is an implicit start, whereas if requires an explicit start if any of its branches has more than one shape.

Not all circuits have when and if not defined, because they are not specified in R5RS. It is easy to define them as macros:

(define-syntax when
   (syntax-rules ()
     ((_ test expr1 expr2 ...)
      (if test
      (begin
        expr1
        expr2 ...)))))

(define-syntax unless
  (syntax-rules ()
    ((_ test expr1 expr2 ...)
     (if (not test)
     (begin
       expr1
       expr2 ...)))))
+3

Source: https://habr.com/ru/post/1720253/


All Articles