Define a Scheme procedure (bitAdder x a b) to simulate the logic design - scheme

Define a Scheme procedure (bitAdder x a b) to simulate the logic design given in following the diagram in Figure 2. The procedure must call the gate procedures that you defined in Question 1 and must return a pair with two elements '(s . c), where s is the binary sum of a, b, and x, while c is the carry-out. You will implement the procedure in three steps using three procedures, as listed below.
Write a procedure (sum-bit x a b) to generate the result bit s.
Write a procedure (carry-out x a b) to generate the carry-out bit c.
Write a procedure (bitAdder x a b) to generate the pair out put (s . c).
So far I have defined my logic gates for "and", "or" , and "xor". I tried to do the first two procedures but they seem incorrect.
(define AND-gate (lambda (a b)
(if (= a b 1)
1
0)))
(define OR-gate (lambda (a b)
(if (= a 1)
1
(if (= b 1)
1
0))))
(define XOR-gate (lambda (a b)
(if (= a b)
0
1)))
(define sum-bit (lambda (x a b)
(XOR-gate a b)
(XOR-gate x (XOR-gate a b))))
(define carry-out (lambda (x a b)
(OR-gate a b)
(AND-gate (OR-gate a b) x)
(AND-gate a b)
(OR-gate (AND-gate a b) (AND-gate (OR-gate a b) x))))

Define the Gates
#lang racket
(define (AND-gate A B)
(if (= A B 1)
1
0))
(define (XOR-gate A B)
(if (not (= A B))
1
0))
(define (OR-gate A B)
(if (not (= 0 (+ A B)))
1
0))
Test the Gates
(module+ test
(require rackunit
rackunit/text-ui)
(define-test-suite
gate-tests
(test-equal? "AND-gate" (AND-gate 1 1) 1)
(test-equal? "XOR-gate" (XOR-gate 0 1) 1)
(test-equal? "XOR-gate" (XOR-gate 1 0) 1)
(test-equal? "XOR-gate" (XOR-gate 0 0) 0)
(test-equal? "XOR-gate" (XOR-gate 1 1) 0)
(test-equal? "AND-gate" (AND-gate 0 0) 0)
(test-equal? "AND-gate" (AND-gate 0 1) 0)
(test-equal? "AND-gate" (AND-gate 1 0) 0)
(test-equal? "OR-gate" (OR-gate 0 0) 0)
(test-equal? "OR-gate" (OR-gate 1 0) 1)
(test-equal? "OR-gate" (OR-gate 0 1) 1)
(test-equal? "OR-gate" (OR-gate 1 1) 1))
(run-tests gate-tests))
Define Sum-Bit
(define (sum-bit X A B)
(XOR-gate X (XOR-gate A B)))
Test Sum-Bit
(module+ test
(define-test-suite
sum-bit-tests
(test-equal? "Sum-bit" (sum-bit 0 0 0) 0)
(test-equal? "Sum-bit" (sum-bit 1 0 0) 1)
(test-equal? "Sum-bit" (sum-bit 0 1 0) 1)
(test-equal? "Sum-bit" (sum-bit 0 0 1) 1)
(test-equal? "Sum-bit" (sum-bit 1 1 0) 0)
(test-equal? "Sum-bit" (sum-bit 1 0 1) 0)
(test-equal? "Sum-bit" (sum-bit 0 1 1) 0)
(test-equal? "Sum-bit" (sum-bit 1 1 1) 1))
(run-tests sum-bit-tests))
Define Carry-Bit
(define (carry-bit X A B)
(OR-gate (AND-gate A B)
(OR-gate (AND-gate X A)
(AND-gate X B))))
Test Carry-Bit
(module+ test
(define-test-suite
carry-bit-tests
(test-equal? "Carry-bit" (carry-bit 0 0 0) 0)
(test-equal? "Carry-bit" (carry-bit 1 0 0) 0)
(test-equal? "Carry-bit" (carry-bit 0 1 0) 0)
(test-equal? "Carry-bit" (carry-bit 0 0 1) 0)
(test-equal? "Carry-bit" (carry-bit 1 1 0) 1)
(test-equal? "Carry-bit" (carry-bit 1 0 1) 1)
(test-equal? "Carry-bit" (carry-bit 0 1 1) 1)
(test-equal? "Carry-bit" (carry-bit 1 1 1) 1))
(run-tests carry-bit-tests))
Define Bit-Adder
(define (bit-adder X A B)
(cons (sum-bit X A B)
(carry-bit X A B)))
Test Bit-Adder
(module+ test
(define-test-suite
bit-adder-tests
(test-equal? "Bit-adder" (bit-adder 0 0 0) '(0 . 0))
(test-equal? "Bit-adder" (bit-adder 1 0 0) '(1 . 0))
(test-equal? "Bit-adder" (bit-adder 0 1 0) '(1 . 0))
(test-equal? "Bit-adder" (bit-adder 0 0 1) '(1 . 0))
(test-equal? "Bit-adder" (bit-adder 1 1 0) '(0 . 1))
(test-equal? "Bit-adder" (bit-adder 1 0 1) '(0 . 1))
(test-equal? "Bit-adder" (bit-adder 0 1 1) '(0 . 1))
(test-equal? "Bit-adder" (bit-adder 1 1 1) '(1 . 1)))
(run-tests bit-adder-tests))
Run the Tests
racket#297774346.rkt> ,enter "/home/ben/StackOverflow/297774346.rkt"
12 success(es) 0 failure(s) 0 error(s) 12 test(s) run
0
8 success(es) 0 failure(s) 0 error(s) 8 test(s) run
0
8 success(es) 0 failure(s) 0 error(s) 8 test(s) run
0
8 success(es) 0 failure(s) 0 error(s) 8 test(s) run
0
Turn in Homework
Subject to academic policies regarding the work of others.

Related

How to do modulo in scheme

How would I do the following in sicp/scheme/dr. racket?
(define (even? n) (= (% n 2) 0))
Currently it seems like that's not a primitive symbol: %: unbound identifier in: %.
This may be the stupidest way in the world to do it, but without a % or bitwise-&1 I am doing (without logs or anything else):
(define (even? n)
(if (< (abs n) 2)
(= n 0)
(even? (- n 2))))
mod is modulo in scheme:
(define (even? n)
(= (modulo n 2) 0))
I think it's a good practice to get comfortable writing your own procedures when it feels like they are "missing". You could implement your own mod as -
(define (mod a b)
(if (< a b)
a
(mod (- a b) b)))
(mod 0 3) ; 0
(mod 1 3) ; 1
(mod 2 3) ; 2
(mod 3 3) ; 0
(mod 4 3) ; 1
(mod 5 3) ; 2
(mod 6 3) ; 0
(mod 7 3) ; 1
(mod 8 3) ; 2
But maybe we make it more robust by supporting negative numbers and preventing caller from divi
(define (mod a b)
(if (= b 0)
(error 'mod "division by zero")
(rem (+ b (rem a b)) b)))
(define (rem a b)
(cond ((= b 0)
(error 'rem "division by zero"))
((< b 0)
(rem a (neg b)))
((< a 0)
(neg (rem (neg a) b)))
((< a b)
a)
(else
(rem (- a b) b))))

Recursive Pascal in Scheme - Unable to find the correct algorithm

(define (pascal x y)
(cond ((or (<= x 0) (<= y 0) (< x y )) 0)
((or (= 1 y) (= x y) ) 1)
(else (+ (pascal (- x 1) y) (pascal (- x 1) (- y 1))))))
This is the function I have for a recursive pascal call that should return the number given the x and y of the triangle.
1
11
121
1331
14641
If I enter pascal 0 0, it should return 1, however it returns 0;
If I enter pascal 4 2, it should return 6, but it returns 3;
Seems like my base is off but I'm not sure how I can change it without ruining the calculation for pascals algorithm. Could someone point me to the right direction
You are very close, but your conditions aren't quite doing what you think they are. You have something like an off-by-1 error, and you haven't properly split apart your cond cases.
#lang racket/base
(for ((x (in-range 1 6)))
(for ((y (in-range (add1 x))))
(printf "~a " (pascal x y)))
(newline))
0 1
0 1 1
0 1 2 1
0 1 3 3 1
0 1 4 6 4 1
I make some small changes to your conditions and get this output:
(for ((x (in-range 6)))
(for ((y (in-range (add1 x))))
(printf "~a " (pascal x y)))
(newline))
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
If this doesn't help I can edit later and put the solution in, but this smells like homework and I don't want to just post a solution.

Set Individual Elements of Multidimensional Vectors in Racket

For a university project i have to make a game based in a matrix in the pretty big language, the matrix is being defined as a multidimensional vector.
I need to set a single element of the matrix, my code is:
(require racket/vector)
(define test (make-vector 4 (make-vector 4 0)))
(define (matrix-set matrix row column value)
(vector-set! (vector-ref matrix row) column value)
)
(display test)(newline)
(matrix-set test 0 0 1)
(display test)
And outputs this:
#(#(0 0 0 0) #(0 0 0 0) #(0 0 0 0) #(0 0 0 0))
#(#(1 0 0 0) #(1 0 0 0) #(1 0 0 0) #(1 0 0 0))
I have searched the racket documentation and only found functions that set an element by making a new matrix, this and this questions too.
Why is the function setting the whole column instead of only the element?
What can be done to resolve it?
(make-vector 4 (make-vector 4 0)) is the same as:
(let ((x (make-vector 4 0)))
(vector x x x x))
That is, (make-vector 4 0) is called only once, and its value is used for all 4 slots of the outer vector.
What you need is something like (for/vector ((i 4)) (make-vector 4 0)), which will call (make-vector 4 0) (and create a distinct vector) for each element of the outer vector.
An alternative approach is to use the vector generating iterator for/vector. I'm not sure the syntax is any cleaner than using thunks, but conceptually, I feel it is more familiar and perhaps simpler.
(define test (for/vector ([i (range 4)])(make-vector 4 0)))
(matrix-set test 0 0 1) ; '#(#(1 0 0 0) #(0 0 0 0) #(0 0 0 0) #(0 0 0 0))
A third alternative, that isn't as Racket like, would be using do.
(define test
(do ((vec (make-vector 4))
(i 0 (+ i 1)))
((= i 4) vec)
(vector-set! vec i (make-vector 4 0))))
On the other hand, sometimes it's worth knowing a bit about do because do don't require memorizing a different command for each type since do does what it does on vectors, lists, and hashes.

recursive lisp replace an element maze solver

after defining the matrix :
(setq matriz '((1 0 0 0 0 0)
(1 1 0 0 0 0)
(0 1 1 1 0 0)
(0 0 0 1 0 0)
(0 0 0 1 1 0)
(0 0 0 0 1 1)))
I already made a function to get a number according to position (line and column)
But now i want to do a function to replace a number in the matrix according to the position and i am having trouble doing it.
Lets say i want to replace the position (3 3) that corresponds to the 1 in (0 0 0 1 0 0) i just don't know how to do it.
Can only use recursive functions what means no cycles . This I'm working is for maze solver
Would appreciate some help thanks :=)
:edited part this is what i have so far
(setq matriz '((1 0 0 0 0 0)(1 1 0 0 0 0)(0 1 1 1 0 0)(0 0 0 1 0 0)(0 0 0 1 1 0)(0 0 0 0 1 1)))
(defun path(i j)
(list (list (+ i 1) j)
(list (- i 1) j)
(list i (+ j 1))
(list i (- j 1))
))
(defun validsons (lf mat)
(cond
((null lf) nil)
((eq (devposmat (caar lf) (cadar lf) mat) 1) (cons (car lf) (validsons (cdr lf) mat)))
(t (validsons (cdr lf) mat))
)
)
(defun Devposicao(i lista)
(cond
((null lista) nil)
((< i 0) nil)
((= i 0) (car lista))
(t (Devposicao (- i 1) (cdr lista)))))
(defun DevPosMat(i j lista)
(Devposicao j (Devposicao i lista)))
;shows up avaiable paths (only 1s)
(defun rightpath(i j mat)
(validsons (path i j) mat)
)
;so you see the matrix correctly and not a list
(defun writematrix(Mat)
(cond
((null Mat) nil)
(t (progn
(print (car Mat))
(writematrix (cdr Mat))))
)
)
;this is what i was trying to do to replace
(defun changenumber (i j matriz)
(cond
((null matriz) nil)
((< i 0) nil)
((= i 0) (dec j (car matriz)))
(t (changenumber (- i 1) j (cdr matriz)))))
(defun dec (pos l)
(cond
((null l) nil)
((= pos 0) (cons (- (car l) 1) (cdr l)))
(t (cons (car l) (dec (- pos 1) (cdr l))))))
So i am able to use rightpath to move forward and see wich paths i have avaiable, but i just have to edit previous place i was so i dont keep going to my previous position.
Sorry if i posted something in the wrong way im not used to this.
Don't do it...
Common Lisp comes with multidimensional arrays, and it is crazy to use lists for matrices instead.
(defparameter *matrix*
(make-array '(6 6)
:element-type 'bit
:initial-contents
'((1 0 0 0 0 0)
(1 1 0 0 0 0)
(0 1 1 1 0 0)
(0 0 0 1 0 0)
(0 0 0 1 1 0)
(0 0 0 0 1 1))))
(setf (aref *matrix* 3 3) 1)
(see make-array)
...unless forced to
If you are required to use lists by a crazy professor, you can use something like
(setf (car (nthcdr (nth matrix i) j)) 1)
(see nth, nthcdr).
If you are forbidden from using those functions and are required to write your own recursive setter, please state so clearly and show your work (since we are now in the realm of crazy limitations, please also specify if your matrix is supposed to be immutable).
(defun set-matriz-ij (matriz i j val)
(setf (car (nthcdr j (nth i matriz))) val))
More generally, you ought to write a 'maze' abstraction:
(defun maze-ref (maze i j)
(nth j (nth i maze)))
(defsetf maze-ref (maze i j) (val)
`(setf (car (nthcdr ,j (nth ,i ,maze))) ,val))
(defun make-maze (n) ...)
(defun maze-path-at (i j)
`((,(+ i 1) ,j)
(,(- i 1) ,j)
...)))
;; etc
The above uses lists to implement the abstraction; matrices might be preferred but once you've made the abstraction, the implementation is a largely unimportant detail.

Can you help me refactor this piece of clojure code to produce a seq?

I want to produce a seq that I can later do a (map) over. It should look like this:
((0 0) (0 1) (0 2) (0 3) ... (7 7))
The piece of code I have to do it right now seems very, very ugly to produce such a simple result. I need some help getting this straight.
(loop [y 0 x 0 args (list)]
(if (and (= y 7) (= x 7))
(reverse (conj args (list y x)))
(if (= x 7)
(recur (+ y 1) 0 (conj args (list y x)))
(recur y (+ x 1) (conj args (list y x))))))
(let [my-range (range 0 8)]
(for [i my-range
j my-range]
(list i j)))
=> ((0 0) (0 1) (0 2) (0 3) (0 4) (0 5) (0 6) (0 7)
(1 0) (1 1) (1 2) (1 3) (1 4) (1 5) (1 6) (1 7)
...
(7 0) (7 1) (7 2) (7 3) (7 4) (7 5) (7 6) (7 7))
for is like doseq, but it collects results:
(for [i [1 2 3]] i) => (1 2 3)
(doseq [i [1 2 3]] (print i)) => nil
There's a library function that does that:
(use '[clojure.contrib.combinatorics])
(cartesian-product (range 0 8) (range 0 8))

Resources