开发者

PLT Scheme noob: Boolean and/or aren't procedures?

I'm trying to make a truth-table generator for a digital electronics course because that's how I have fun in my spare time and don't judge me.

Anywho, I figured I'd have a hash with the string equivalent of operators as keys, and the Scheme procedures that correspond to those operators as values.

E.g.

(define  operator-table #hash(("+" . or)
                              ("开发者_运维技巧*" . and)))

So I could do something like

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

Now I realize the above probably won't work right, but as it is I can't even tinker with it until I get it right, because apparently and and or are special in Scheme. At the REPL, if I type in not it replies #<procedure:not>. But if I give it and or or, it says and: bad syntax in: and. Is there a procedure version of and I can use? Or do I have to make one with lambda? Or am I missing something entirely?

The only reason I didn't just go with lambda from the get-go is that I don't want to lose the variable-arity abilities of the builtin and [I can do (and #t #f) as well as (and #t #f #f #t #f #t)].


If you want to use a function form of and and or (with the limitation that they will not short-circuit) and not lose the variable arity property, then you can easily do so:

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

(values is the idiomatic identity function in (PLT) Scheme.)


Yep, they can't be procedures because Scheme (like most languages) is an "eager" language -- every argument to a procedure gets evaluated before the procedure takes control (receiving the argument values). and and or need to "short-circuit" -- stop argument evaluation as soon as the final result is known -- so they can't be regular procedures/functions in any eager language ("lazy" languages, such as Haskell -- where each argument is evaluated only if, when and as needed -- are a very different breed, and don't need this "regular function" versus "special form" distinction... but, alas, Scheme just isn't one of those!).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜