I have a procedure with no arguments that creates a matrix, but returns nothing, how can i access the matrix? - scheme

I have a procedure with no arguments that creates a matrix, but returns nothing, how can i access the matrix?
This is my code:
(define matrix
(lambda (rows columns)
(do ((m (make-vector rows))
(i 0 (+ i 1)))
((= i rows) m)
(vector-set! m i (make-vector columns)))))
(define Mod-matrix!
(lambda (m i j)
(vector-ref (vector-ref m i) j)))
(define (board)
(mk-w (matrix 8 8) 0 0))
(define (mk-b b l c)
(cond ((and (< l 8) (< c 8)) (begin
(Mod-matrix! b l c p)
(mk-b b l (+ c 2))))
((and (>= c 8) (< l 8))(mk-b b (+ l 2) 0))
(else (mk-w b 0 1))))
(define (mk-b b l c)
(cond ((and (< l 8) (< c 8)) (begin
(Mod-matrix! b l c b)
(mk-w ti l (+ c 2))))
((and (>= c 8) (< l 8)) (mk-white b (+ l 2) 0))))

Your mk-w function is not returning the matrix. Try the following:
(define (mk-w b l c)
(cond
((and (< l 8) (< c 8))
(begin (Mod-matrix! b l c)
(mk-w b l (+ c 2))
b))
((and (>= c 8) (< l 8))
(begin
(mk-w b (+ l 2) 0)
b))))
Note that in each case the last expression in the begin block is the matrix itself, this is because the return value of a begin block is the value of the last expression. In your post, those last expressions returned an undefined value.

If you want to access the matrix, have the function return the matrix rather than nil or whatever. It's generally considered bad form, but you could set! the matrix to some variable inside the function.
If you can't change the function, you can't access the matrix.

Related

Trying to write hyper-operations in scheme

I am trying to write a hyperoperation program in MIT/GNU-Scheme however am having some trouble, I have written individual ones up to n=5 working but would like to make one that functions does them all. I will include some of my failed attempts below.
(define hyp (lambda (n x y)
(hypiter n x x y)))
(define hypiter (lambda (lev an cou lim)
(if (= lev 1) an
(hypiter lev (hyp (- lev 1) an cou) cou lim))))
(define hyper (lambda (n x y)
(hyper n x x y)))
(define hyperiter (lambda (lev an cou lim)
(if (= lev 1) (+ an cou)
(hyper (- lev 1) an cou))))
(define h (lambda (n a b)
(cond
((= n 1) (+ a b))
((= b 1) (- n 1))
(else (h (- n 1) (h (- n 1) a a) (- b 1)))))))
(define hyperoperation (lambda (n a b)
(cond
((= n 0) (+ 1 b))
((and (= n 1) (= b 0)) a)
((and (= n 2) (= b 0)) 0)
((and (>= n 3) (= b 0)) 1)
(else (hyperoperation (- b 1) a (hyperoperation n a (- b 1)))))))
According to the definition in wikipedia, there is an error in the last line of your last definition. It should be:
(else (hyperoperation (- n 1) a (hyperoperation n a (- b 1))))))
instead of:
(else (hyperoperation (- b 1) a (hyperoperation n a (- b 1)))))))
So a possible correct recursive definition could be:
(define (hyperoperation n a b)
(cond ((= n 0) (+ b 1))
((= b 0) (cond ((= n 1) a)
((= n 2) 0)
(else 1)))
(else (hyperoperation (- n 1) a (hyperoperation n a (- b 1))))))

Extended Euclidian Algorithm in Scheme

I'm trying to write a code for extended Euclidian Algorithm in Scheme for an RSA implementation.
The thing about my problem is I can't write a recursive algorithm where the output of the inner step must be the input of the consecutive outer step. I want it to give the result of the most-outer step but as it can be seen, it gives the result of the most inner one. I wrote a program for this (it is a bit messy but I couldn't find time to edit.):
(define ax+by=1
(lambda (a b)
(define q (quotient a b))
(define r (remainder a b))
(define make-list (lambda (x y)
(list x y)))
(define solution-helper-x-prime (lambda (a b q r)
(if (= r 1) (- 0 q) (solution-helper-x-prime b r (quotient b r) (remainder b r)))
))
(define solution-helper-y-prime (lambda (a b q r)
(if (= r 1) (- r (* q (- 0 q) )) (solution-helper-y-prime b r (quotient b r) (remainder b r))
))
(define solution-first-step (lambda (a b q r)
(if (= r 1) (make-list r (- 0 q))
(make-list (solution-helper-x-prime b r (quotient b r) (remainder b r)) (solution-helper-y-prime b r (quotient b r) (remainder b r))))
))
(display (solution-first-step a b q r))
))
All kinds of help and advice would be greatly appreciated. (P.S. I added a scrrenshot of the instructions that was given to us but I can't see the image. If there is a problem, please let me know.)
This is a Diophantine equation and is a bit tricky to solve. I came up with an iterative solution adapted from this explanation, but had to split the problem in parts - first, obtain the list of quotients by applying the extended Euclidean algorithm:
(define (quotients a b)
(let loop ([a a] [b b] [lst '()])
(if (<= b 1)
lst
(loop b (remainder a b) (cons (quotient a b) lst)))))
Second, go back and solve the equation:
(define (solve x y lst)
(if (null? lst)
(list x y)
(solve y (+ x (* (car lst) y)) (cdr lst))))
Finally, put it all together and determine the correct signs of the solution:
(define (ax+by=1 a b)
(let* ([ans (solve 0 1 (quotients a b))]
[x (car ans)]
[y (cadr ans)])
(cond ((and (= a 0) (= b 1))
(list 0 1))
((and (= a 1) (= b 0))
(list 1 0))
((= (+ (* a (- x)) (* b y)) 1)
(list (- x) y))
((= (+ (* a x) (* b (- y))) 1)
(list x (- y)))
(else (error "Equation has no solution")))))
For example:
(ax+by=1 1027 712)
=> '(-165 238)
(ax+by=1 91 72)
=> '(19 -24)
(ax+by=1 13 13)
=> Equation has no solution

Scheme quadratic function/square root check

Im want to make a function where rootcheck has a list L as input, L always is 3 atoms (a b c) where a is coefficient of x^2, b coef of x and c is the constant. it checks if the equation is quadratic, using discriminant (b^2 - 4ac) and should output this (num 'L) where num is the number of roots and L is a list that contains the roots themselves (using quadratic formula), L is empty in case of no roots. here is my code:
(define roots-2
(lambda (L)
(let ((d (- (* (cdr L) (cdr L)) (4 (car L) (caddr L))))))
(cond ((< d 0) (cons(0 null)))
((= d 0) (cons(1 null)))
(else((> d 0) (cons(2 null)))))
))
its giving me no expression in body error.
also I tried to code the quadratic function and even tried some that are online, one compiled fint but gave me an error when I inserted input this is the code for the quadratic function, NOT MINE!
(define quadratic-solutions
(lambda (a b c) (list (root1 a b c) (root2 a b c))))
(define root1
(lambda (a b c) (/ (+ (- b) (sqrt (discriminant a b c)))
(* 2 a))))
(define root2
(lambda (a b c) (/ (- (- b) (sqrt (discriminant a b c)))
(*2 a))))
(define discriminant
(lambda (a b c) (- (square b) (* 4 (* a c)))))
There are several mistakes in the code:
Some parentheses are incorrectly placed, use a good IDE to detect such problems. This is causing the error reported, the let doesn't have a body
You forgot to multiply in the 4ac part
You're incorrectly accessing the second element in the list
The else part must not have a condition
The output list is not correctly constructed
This should fix the errors, now replace null with the actual call to the function that calculates the roots for the second and third cases (the (< d 0) case is fine as it is):
(define roots-2
(lambda (L)
(let ((d (- (* (cadr L) (cadr L)) (* 4 (car L) (caddr L)))))
(cond ((< d 0) (list 0 null))
((= d 0) (list 1 null))
(else (list 2 null))))))
for the quadractic function part, I found a code online and tweaked it to provide both roots of a quadratic equation. returns a list of both roots
(define (solve-quadratic-equation a b c)
(define disc (sqrt (- (* b b)
(* 4.0 a c))))
(list (/ (+ (- b) disc) (* 2.0 a))
(/ (- (- b) disc) (* 2.0 a))
))

(scheme) How to add elements in a matrix?

This function is supposed to find the sum of each row and put it in a list. I thought something like this would work but it doesn't. It gives me a weird output.
Like, if I have a matrix that has two rows and two columns of 1's, it returns this:
(2 . 1)
Instead of this:
(2 2)
Help?
(define (sum mat)
(let loop ([r 0]
[c 0])
(if (> r (matrix-rows mat)) '()
(if (>= c (sub1 (matrix-cols mat))) (add1 r)
(cons (+ (matrix-ref mat r c) (matrix-ref mat r (add1 c))) (loop r (add1 c)))))))
Instead of calling (add1 r) you should call (loop (+ r 1) 0). Note: this suggestion is correct; however, there are likely other errors in your code, specifically your computation with the matrix-ref calls doesn't look like it will add up a row. You can see that by testing with a matrix of more than two rows.
Here is a fix:
(define (sum mat)
(let loop ((r 0) (c 0) (s 0) (a '()) ;; row, col, sum, ans
(cond ((>= r (matrix-rows mat)) (reverse a))
((>= c (matrix-cols mat)) (loop (+ r 1) 0 0 (cons s a)))
(else (loop r (+ c 1) (+ s (matrix-ref mat r c)) a)))))

Miller-Rabin Scheme implementation unpredictable output

I am new to Scheme. I have tried and implemented probabilistic variant of Rabin-Miller algorithm using PLT Scheme. I know it is probabilistic and all, but I am getting the wrong results most of the time. I have implemented the same thing using C, and it worked well (never failed a try). I get the expected output while debugging, but when I run, it almost always returns with an incorrect result. I used the algorithm from Wikipedia.
(define expmod( lambda(b e m)
;(define result 1)
(define r 1)
(let loop()
(if (bitwise-and e 1)
(set! r (remainder (* r b) m)))
(set! e (arithmetic-shift e -1))
(set! b (remainder (* b b) m))
(if (> e 0)
(loop)))r))
(define rab_mil( lambda(n k)
(call/cc (lambda(breakout)
(define s 0)
(define d 0)
(define a 0)
(define n1 (- n 1))
(define x 0)
(let loop((count 0))
(if (=(remainder n1 2) 0)
(begin
(set! count (+ count 1))
(set! s count)
(set! n1 (/ n1 2))
(loop count))
(set! d n1)))
(let loop((count k))
(set! a (random (- n 3)))
(set! a (+ a 2))
(set! x (expmod a d n))
(set! count (- count 1))
(if (or (= x 1) (= x (- n 1)))
(begin
(if (> count 0)(loop count))))
(let innerloop((r 0))
(set! r (+ r 1))
(if (< r (- s 1)) (innerloop r))
(set! x (expmod x 2 n))
(if (= x 1)
(begin
(breakout #f)))
(if (= x (- n 1))
(if (> count 0)(loop count)))
)
(if (= x (- s 1))
(breakout #f))(if (> count 0) (loop count)))#t))))
Also, Am I programming the right way in Scheme? (I am not sure about the breaking out of loop part where I use call/cc. I found it on some site and been using it ever since.)
Thanks in advance.
in general you are programming in a too "imperative" fashion; a more elegant expmod would be
(define (expmod b e m)
(define (emod b e)
(case ((= e 1) (remainder b m))
((= (remainder e 2) 1)
(remainder (* b (emod b (- e 1))) m)
(else (emod (remainder (* b b) m) (/ e 2)))))))
(emod b e))
which avoids the use of set! and just implements recursively the rules
b^1 == b (mod m)
b^k == b b^(k-1) (mod m) [k odd]
b^(2k) == (b^2)^k (mod m)
Similarly the rab_mil thing is programmed in a very non-scheme fashion. Here's an alternative implementation. Note that there is no 'breaking' of the loops and no call/cc; instead the breaking out is implemented as a tail-recursive call which really corresponds to 'goto' in Scheme:
(define (rab_mil n k)
;; calculate the number 2 appears as factor of 'n'
(define (twos-powers n)
(if (= (remainder n 2) 0)
(+ 1 (twos-powers (/ n 2)))
0))
;; factor n to 2^s * d where d is odd:
(let* ((s (twos-powers n 0))
(d (/ n (expt 2 s))))
;; outer loop
(define (loop k)
(define (next) (loop (- k 1)))
(if (= k 0) 'probably-prime
(let* ((a (+ 2 (random (- n 2))))
(x (expmod a d n)))
(if (or (= x 1) (= x (- n 1)))
(next)
(inner x next))))))
;; inner loop
(define (inner x next)
(define (i r x)
(if (= r s) (next)
(let ((x (expmod x 2 n)))
(case ((= x 1) 'composite)
((= x (- n 1)) (next))
(else (i (+ 1 r) x))))
(i 1 x))
;; run the algorithm
(loop k)))

Resources