PLT Scheme noob: logical and / or not procedures?

I am trying to create a truth table generator for a digital electronics course, because this is how I have fun in my free time and do not judge myself.

Anywho, I decided that I would have a hash with the equivalent of operator strings as keys and procedure schemas that match these operators as values.

eg.

  (define operator-table #hash (("+". or)
                               ("*". and)))

So that I could do something like

(lambda (ab) ((hash-ref operator-table (string-ref input-str i)) ab)) 

Now I understand that the above probably won't work right, but since I can't even mess around with it until I get it right, because obviously and and or are special in the Scheme. In REPL, if I type not , it answers #<procedure:not> . But if I give him and or or , he says and: bad syntax in: and . Is there a version of and procedure that I can use? Or do I need to do one with lambda ? Or am I missing something?

The only reason I don’t just go with the lambda from get-go is because I don’t want to lose the ability of the -arity variable of the built-in and [I can do (and #t #f) as well (and #t #f #f #t #f #t) ].

+4
source share
2 answers

If you want to use the form of the and and or function (with the restriction that they will not be shorted) and not lose the property of the arity variable, you can easily do this:

 (define (and* . xs) (andmap values xs)) (define (or* . xs) (ormap values xs)) 

( values is an idiomatic identity function in a circuit (PLT).)

+6
source

Yes, they cannot be procedures, because Scheme (like most languages) is an "impatient" language - each argument to a procedure is evaluated before the procedure takes control (by getting the values ​​of the argument). and and or need a "short circuit" - stop evaluating the argument as soon as the final result is obtained - therefore they cannot be regular procedures / functions in any impatient language ("lazy" languages ​​like Haskell, where each argument is evaluated only in In that case, when and when it is necessary, it is a completely different breed and does not need this "regular function" in comparison with the difference of the "special form" ... but, alas, Scheme just isn’t one of those!) .

+3
source

All Articles