Scheme programming. If someone could help me out with these two functions that w
ID: 3793076 • Letter: S
Question
Scheme programming. If someone could help me out with these two functions that would be great.
Write a function, infix->prefix, which takes an infix arithmetic expression and returns the corresponding prefix expression. > (infix->prefix 42) 42 > (infix->prefix '(1 + 2)) (+ 1 2) > (infix->prefix '(1 + (2 * 8))) (+ 1 (* 2 8)) > (infix->prefix '((((2 + 3) * 2)/5) + (17 - 1)) (+ (/(* (+ 2 3) 2) 5) (- 17 1)) Define a function iota-iota that takes an integer i as its argument and returns a list of pairs of integers such that > (iota-iota 1) ((1. 1)) > (iota-iota 2) ((1. 1) (1. 2) (2. 1) (2 .2)) > (iota-iota 3) ((1. 1) (1. 2) (1 .3) (2. 1) (2 .2) (2. 3) (3 .1) (3. 2) (3. 3))Explanation / Answer
(load "test.scm")
(load "homework2.scm")
(test (infix->prefix 42) 42)
(test (infix->prefix '(1 + 2)) '(+ 1 2))
(test (infix->prefix '(1 + (2 * 8))) '(+ 1 (* 2 8)))
(test (infix->prefix '((((2 + 3) * 2) / 5) + (17 - 1)))
'(+ (/ (* (+ 2 3) 2) 5) (- 17 1)))
(test (iota-iota 0) '())
(test (iota-iota 1) '((1 1)))
(test (iota-iota 2) '((1 1) (1 2) (2 1) (2 2)))
(test (iota-iota 3) '((1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3)))
homework2.scm
;; Additional Problem 2
(define infix->prefix
(lambda (ls)
(cond
((null? ls) '())
((number? ls) ls)
((and (pair? (car ls)) (pair? (caddr ls)))
(cons (cadr ls) (cons (infix->prefix (car ls))(list (infix->prefix (caddr ls))))))
((pair? (car ls))
(cons (cadr ls) (cons (infix->prefix (car ls)) (list (caddr ls)))))
((pair? (caddr ls))
(cons (cadr ls) (cons (car ls) (list (infix->prefix (caddr ls))))))
(else
(cons (cadr ls) (cons (car ls) (list (caddr ls))))))))
;; Additional Problem 3
(define iota-iota
(lambda (n)
(letrec ((loop
(lambda (x y acc)
(cond
((> x n) acc)
((> y n) (loop (add1 x) 1 acc))
(else (loop x (add1 y) (append acc (list (list x y)))))))))
(loop 1 1 '()))))
test.scm
(define test
(lambda (actual expected)
(if (equal? actual expected) (displayln "pass") (displayln "fail"))))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.