How to design a function that takes two Boolean parameters and returns true if either (or both) of the parameters are true? - boolean

;Boolean b1 b2:
;#true & #true => #true
;#true & #false => #true
;#false & #false => #false
I kind of know how it works, but I don't know how to write it in Racket. I should have two parameters in a function, but how can I write that in Racket. Should it starts like
(define (either-true? b1 b2)
())

Other
answers
show that the body of either-true? could be just
(or b1 b2) or (if b1 #t b2) (or even (if b2 #t b1) )
A literal implementation of the "purpose" of either-true? could be:
(define (either-true? b1 b2) ;; Boolean Boolean -> Boolean
;; produce #t if either b1 or b2 is true, otherwise #f
(cond
[ b1 #t ]
[ b2 #t ]
[else #f ]))
In the Racket student languages, conditional forms (if, cond, or, etc) require their
"question-expressions" to be Boolean values (#t or #f). Other Racket languages use
the Scheme interpretation of any non-#f value as true
in conditional contexts.
So yet another definition, using this interpretation of "truthiness", could be:
#lang racket
(define (either-true? b1 b2) ;; Any Any -> Any
;; produce #f if both b1 and b2 #f, otherwise a truthy value
(findf values (list b1 b2)))
It's worth noting that, although (either-true? b1 b2) looks like (or b1 b2), there
is a significant difference: or will not evaluate b2 if b1 is true; either-true? always
evaluates both arguments. The difference can be seen by running:
#lang racket
(require math/number-theory)
(define (either-true? b1 b2)
(or b1 b2))
(time (or (prime? 7) (prime? (next-prime (expt 2 1000)))))
(time (either-true? (prime? 7) (prime? (next-prime (expt 2 1000)))))

(define (either-true? b1 b2)
(if b1 #t b2))
also make sure you have both of these check-expects:
(check-expect (either-true? #t #f) #t)
(check-expect (either-true? #f #t) #t)
good luck with fundies 1 lol

Your either-true? exists already in Racket and is called or.
(define (either-true? b1 b2) (or b1 b2))
Or just do:
(define either-true? or)

Related

Extending a reduction relation

While taking a look at PLT redex, I wanted to play with simplification rules; so I defined this minimal language for booleans:
(define-language B0
(b T F (not b)))
I wanted to simplify a chain of (not (not ...)) so I extended the language to deal with contexts and defined a reduction relation to simplify the not:
(define-extended-language B1 B0
(C (not C) hole)
(BV T F))
(define red0
(reduction-relation
B1
(--> (in-hole C (not T)) (in-hole C F))
(--> (in-hole C (not F)) (in-hole C T))))
Now I wanted to extend my language to boolean equations and to allow not-simplification at each side of the equation, so I defined:
(define-extended-language B2 B1
(E (= C b) (= b C)))
hoping that:
(define red1
(extend-reduction-relation red0 B2))
will do the thing.
But no: red1 can reduce (not (not (not F))))) but not (= (not T) F)))
Am I doing something really silly here?
The problem with red1 is that it only contains the rules of red0 which use the limited context C. To make it work as expected you could either add the old rules modified to use E or make somehow the final extended context have the name C. One not very tedious approach could be:
(define-language L)
(define R
(reduction-relation L
(--> (not T) F)
(--> (not F) T)))
(define-language LB
(b T F (not b))
(C (compatible-closure-context b)))
(define RB (context-closure R LB C))
(define-extended-language LBE LB
(e (= b b))
(C .... (compatible-closure-context e #:wrt b)))
(define RBE (extend-reduction-relation RB LBE))
Note that this doesn't work in some older versions.
Two sources of useful information are this tutorial and of course the redex reference.

Exercise 1.3 SICP Error: Cannot evaluate expression in Racket

I am doing Exercise 1.3 from SICP.
My code is the following:
#lang racket
(require sicp)
(define (square a)
(* a a)
)
(define (sum-of-squares a b)
(+ (square a) (square b) )
)
(define (max a b)
(cond ((>= a b) a)
(else b)
)
)
(define (sum-of-biggest-squares a b c )
(cond ((>= a b)
(sum-of-squares a (max b c) )
(sum-of-squares b (max a c) )
)
)
)
(sum-of-biggest-squares 5 7 10)
Surprisingly, the Racket interpreter does
not print any result for the above. The interpreter
works fine for other values. But for
this set of three values its not
working.
When I try to add an else statement
like the following:
(else (sum-of-squares b (max a c) ) )
The interpreter says:
exercise_1-3.rkt:23:10: else: not allowed as an expression
in: (else (sum-of-squares b (max a c)))
You have a couple of syntax errors in function sum-of-biggest-squares: a parenthesis should be added to close the first cond clause, and else should be added to the second one:
(define (sum-of-biggest-squares a b c)
(cond ((>= a b) (sum-of-squares a (max b c)))
(else (sum-of-squares b (max a c)))))
Note that the way in which you format the code, so different from the current common conventions, makes very difficult to read it and easy to introduce syntax errors.

define scheme macro for xor

Notice the following macro is working.
(define-syntax xor
(syntax-rules ()
((xor a1 a2)
(if a1
(false? a2)
(true? a2)))
((xor a1 a2 a3 ...)
(let ((a (xor a1 a2)))
(xor a a3 ...)))))
However it seems if I leave out the a3 in the parameter list of the general case, it won't work properly.
(define-syntax xor
(syntax-rules ()
((xor a1 a2)
(if a1
(false? a2)
(true? a2)))
((xor a1 a2 ...)
(let ((a (xor a1 a2)))
(xor a ...)))))
Thus I am wondering what is going on exactly to the ellipsis in the second case.
Q1. Does that mean the each parameter list excluding the ellipsis should be unique for it to run properly?
For example given input (xor #t #t #t), the first will produce #t, whereas the second will produce #f.
The interpreter I am using is mit-scheme.
Q2. And is it possible to make it short circuiting?
Thanks,
The ellipses tells you something about the second symbol. a2 ... can be zero or more elements and you need to use a2 in what is being repeated for it to work. In your second macro the result with a2 is missing ellipsis for the recurring elements and you have ellipsis after a which isn't a part of the match pattern and also does not have ellipsis. Both of these facts make the macro invalid.
The first macro is correct since you have one term that matches two elements. Your second term also matches two terms, but since the first pattern that matches is run you are sure you have more than two arguments for your second pattern since it matched two with a3 ... being at least one element.
I'm not sure what the true? is for. A slight simplification:
(define-syntax xor
(syntax-rules ()
((xor a1 a2)
(if a1 (not a2) a2))
((xor a1 a2 a3 ...)
(xor (xor a1 a2) a3 ...))))
(xor 1 2 3 4 5) ; ==> 5 (odd number of true values)
(xor 1 2 3 4) ; ==> #f (even number of true values)
(xor 1 2 3 4 #f) ; ==> #f (even number of true values)
(xor 1 #f #f #f) ; ==> #t (odd number of true values)
Now this will calculate the odd parity of the argument expressions. It can not be short circuited since it flip flops. (xor #t #t #f #f #t) ; ==> #t since it has an odd number of true arguments. That's about what it does and while it's daisy chaining xor logic it doesn't really have the only one true logic left. Since you can never short circuit it you might as well use a procedure that does the exact same thing:
(define (xor . args)
(= (remainder (count values args) 2) 1))
(xor 1 2 3 4 5) ; ==> #t (odd number of true values)
(xor 1 2 3 4) ; ==> #f (even number of true values)
(xor 1 2 3 4 #f) ; ==> #f (even number of true values)
(xor 1 #f #f #f) ; ==> #t (odd number of true values)
Count can be found in the SRFI-1 list library.
There is another interpretation of xor and that is the first one I though about when I read this question since it's the only case where short circuit works. It is one that is true iff one expression is true, otherwise the result is false. Here, when encountering the second false value you can short circuit to #f without evaluating the rest of the arguments.
(define-syntax xor
(syntax-rules ()
((_) #f)
((_ a) a)
((_ a b ...)
(if a
(not (or b ...))
(xor b ...)))))
(xor 1 2 3 4 5) ; ==> #f (more than one true value)
(xor 1 2 3 4) ; ==> #f (more than one true value)
(xor 1 2 3 4 #f) ; ==> #f (more than one true value)
(xor 1 #f #f #f) ; ==> #t (only one true value)
;; Slightly more complex version where
;; the result is always the one true value or #f
(define-syntax xor
(syntax-rules ()
((_) #f)
((_ a) a)
((_ a b ...)
(let ((tmp a))
(if tmp
(and (not (or b ...)) tmp)
(xor b ...))))))
(xor 1 2 3 4 5) ; ==> #f
(xor 1 2 3 4) ; ==> #f
(xor 1 2 3 4 #f) ; ==> #f
(xor 1 #f #f #f) ; ==> 1 (the actual true value, consistent)
Most algorithms won't have any speed penalties from using a procedure, but I guess there might be a few situations where this macro might be in handy as a macro. The procedure version of the one that doesn't keep the value is very similar to the procedure version of the other one:
(define (xor . args)
(= (count values args) 1))
I don't know how to use fanciful define-syntax but this might offer some help.
If anyone can explain why xor should be defined as syntax instead of a simple procedure, I'd like to know ^_^
(define (xor a b . xs)
(cond [(and a b) #f]
[(empty? xs) (or a b)]
[else (apply xor (or a b) (car xs) (cdr xs))]))
(xor #t #f) ; => #t
(xor #t #t) ; => #f
(xor #t #f #f) ; => #t
(xor #t #f #f #t) ; => #f
(xor #f #f #f #f #f #t) ; => #t

Use conditional places in setf

Lets say I have two variables and I want to set the variable with lower value to nil.
Is it possible to make it work this way?
(setf a1 5)
(setf a2 6)
(setf
(if (< a1 a2) a1 a2)
nil
)
If you want to do something close to this, you can use (setf (symbol-value var) ...):
> (defparameter a1 5)
> (defparameter a2 6)
> (setf (symbol-value (if (< a1 a2) 'a1 'a2)) nil)
> a1
nil
> a2
6
To get a syntax closer to the one in your question, you can define an setf-expanderfor if:
(defsetf if (cond then else) (value)
`(progn (setf (symbol-value (if ,cond ,then ,else)) ,value)
,value))
Then you can write (setf (if (< a1 a2) 'a1 'a2) nil)
However, the best way of writing the code is probably to do it straight forward, using an if with setf forms in both branches:
(if (< a1 a2)
(setf a1 nil)
(setf a2 nil))
No, because that if form is not a place and thus not setfable.
Although it's not very useful by itself, you might want to regard this as a little hint:
(defun new-values (x y)
(if (< x y) (values nil y) (values x nil)))
(setf a1 5)
(setf a2 6)
(setf (values a1 a2) (new-values a1 a2))

Programatically filling in a letrec in Scheme. Macros or eval?

I'm just playing with an NFA for string recognition. I have a macro that creates a function which consumes input and passes on the rest to some other functions. Because there might be loops in my NFA graph, I'm using letrec to put the whole thing together. Here is some code (been testing in PLT-Scheme):
(define-syntax-rule (match chars next accepting)
; a function that consumes a list of chars from a list l.
; on success (if there's more to do) invokes each of next on the remainder of l.
(lambda (l)
(let loop ((c chars) (s l))
(cond
((empty? c)
(cond
((and (empty? s) accepting) #t)
(else
(ormap (lambda (x) (x s)) next))))
((empty? s) #f)
((eq? (car c) (car s))
(loop (cdr c) (cdr s)))
(else #f)))))
; matches (a|b)*ac. e .g. '(a a b b a c)
(define (matches? l)
(letrec
([s4 (match '( ) '() #t)]
[s3 (match '(c) `(,s4) #f)]
[s2 (match '(a) `(,s3) #f)]
[s1 (match '( ) `(,s2 ,s5) #f)]
[s5 (match '( ) `(,s6 ,s7) #f)]
[s6 (match '(a) `(,s8) #f)]
[s7 (match '(b) `(,s8) #f)]
[s8 (match '( ) `(,s1) #f)])
(s1 l)))
(matches? '(a c))
(matches? '(a b b b a c))
(matches? '(z a b b b a c))
Now, what if I had a simple data-structure to represent my NFA, like a list of lists. e.g.
'((s4 () () #t)
(s3 (c) (s4) #f)
...)
My question is: How would I turn that list into the former letrec statement? I'm not too good with Macros and my understanding is that I probably shouldn't be using eval.
If the list is known at compile time (what I mean is, before your program starts running) then you can use a macro. Otherwise you must use eval.
It's ok. This is one of the good uses for eval. :)
I came up with this macro which seems to do the job
(I'm not an expert either):
(define-syntax nfa
(syntax-rules (let-bindings)
; All the let bindings have been expanded
[(nfa start (let-bindings . bindings))
(lambda (l) (letrec bindings (start l)))]
; Otherwise, expand the next binding
[(nfa start (let-bindings . bindings) (s c n a) . rest)
(nfa start (let-bindings (s (match 'c (list . n) a)) . bindings) . rest)]
; Insert the expanded bindings list
[(nfa start states)
(nfa start (let-bindings) . states)]))
; matches (a|b)*ac. e .g. '(a a b b a c)
(define matches?
(nfa s1 ([s4 ( ) () #t]
[s3 (c) (s4) #f]
[s2 (a) (s3) #f]
[s1 ( ) (s2 s5) #f]
[s5 ( ) (s6 s7) #f]
[s6 (a) (s8) #f]
[s7 (b) (s8) #f]
[s8 ( ) (s1) #f])))
The trick is to use intermediate forms to create "subtitution loops",
and reserve identifiers (cf. let-bindings) to distinguish these intermediate forms
from direct usage of the macro.
I think your problem can be seprate into 2 subproblem:
write a macro that consumes a NFA description and generate a NFA automatically,I call this macro make-NFA
apply make-NFA to a list generated programatically,I call this macro apply-macro
the second subproblem is easy:
(define-syntax apply-macro
(syntax-rules ()
((_ macro ls)
(eval
`(macro ,#ls)
(interaction-environment)))))
;(define ls '(1 2 3))
;(apply-macro if ls)=>2
the first question,I have a DFA sample,you can write a NFA by youself:
(define-syntax make-DFA
(syntax-rules (: ->)
((_ init-state (state : result (symbol -> next) ...) ...)
(letrec
((state
(lambda(sigma)
(cond
((null? sigma) result)
(else
(case (car sigma)
((symbol)
(next (cdr sigma)))...
(else false))))))... )
init-state))))
(define DFA1
(make-DFA q1
(q1 : true (#\a -> q2)
(#\b -> q3))
(q2 : false (#\a -> q1)
(#\b -> q4))
(q3 : false (#\a -> q4)
(#\b -> q1))
(q4 : true (#\a -> q3)
(#\b -> q2))))
(DFA1 (string->list "ababa"));=>#f
well,may be define-macro is a better way to implement apply-macro.