what's wrong with this basic scheme function - function

I'm trying to write a scheme function that does the following (taken from SICP):
Exercise 1.3. Define a procedure that takes three numbers as arguments
and returns the sum of the squares of the two larger numbers.
Here's my attempt:
(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c)))
(cond ((and (< b c) (< b a))) (+ (* a a) (* c c)))
(cond ((and (< c a) (< c b))) (+ (* b b) (* a a)))
)
When I enter this into the biwa scheme interpreter, I get the following:
(square-sum-larger 4 5 6)
16
Since 4 is less than 5 and 6, shouldn't the first condition be evaluated, meaning that the sum of the squares of 5 and 6 should be returned?

(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c))) ;; this is thrown away
(cond ((and (< b c) (< b a))) (+ (* a a) (* c c))) ;; this is thrown away
(cond ((and (< c a) (< c b))) (+ (* b b) (* a a)))
)
Only the last of the three cond does anything useful; the previous cond expressions do not have any side effect, since they perform only calculations, and their value is not used. The are dead code which a Scheme compiler can eliminate entirely.
You probably wanted to combine all of the clauses into a single cond:
(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c))
((and (< b c) (< b a))) (+ (* a a) (* c c))
((and (< c a) (< c b))) (+ (* b b) (* a a))))

(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c)))
(cond ((and (< b c) (< b a))) (+ (* a a) (* c c)))
(cond ((and (< c a) (< c b))) (+ (* b b) (* a a)))
)
Is basically this in JS:
function squareSumLarge(a, b, c) {
// not tail position , dead code regardless if the test is truthy
if (a < b && a < c) {
b * b + c * c;
}
// not tail position , dead code regardless if the test is truthy
if (b < c && b < a) {
a * a + c * c;
}
if (c < a && c < b0 {
// return because cond is in tail position
return b * b + a * a;
} else {
// if the last cond term didn't hit it returns an implementation chosen value
return undefined;
}
}
Know that this pattern:
if (test1) {
return expression1;
} elseif (test2) {
return expression2;
} else {
return expression3;
}
is done like this with cond when the cond is in tail position:
(cond
(test1 expression1)
(test2 expression2)
(else expression3))

Related

How can I fix my code to avoid returning duplicate pairs while using map in racket?

This function should return the transitive closure of L. For Examples:
(Transitive-Closure'((a b) (b c) (a c))) ---> '((a b) (b c) (a c))
(Transitive-Closure'((a a) (b b) (c c))) ---> '((a a) (b b) (c c))
(Transitive-Closure'((a b) (b a))) ---> '((a b) (b a) (a a) (b b)))
(Transitive-Closure'((a b) (b a) (a a)))---> '((a b) (b a) (a a) (b b))
(Transitive-Closure'((a b) (b a) (a a) (b b)))---> '((a b) (b a) (a a) (b b))
(Transitive-Closure'())---> '()
Here is what I have in Racket:
(define (Transitive-Closure L)
(apply append
; Iterate over each pair (a b) in L,
(map (lambda (x)
;Iterate over each pair (c d) in L,
(map (lambda (y)
(let ([a (car x)]
[b (cadr x)]
[c (car y)]
[d (cadr y)])
;if b equal to c, and (a d) does not exist in L, it will add (a d) to L . Otherwise, return L.
(if (and (eq? b c) (not (member (list a d) L)))
(list a d)
(append x))))L)) L)))
My code only works when it's not transitive. How can I modify my code to avoid returning duplicate pairs when it's transitive?
For example, my Output:
;This is wrong. It should return '((a b)(b c)(a c))
(Transitive-Closure '((a b)(b c)(a c))) ---> '((a b) (a b) (a b) (b c) (b c) (b c) (a c) (a c) (a c))
; This is right.
(Transitive-Closure '((a b)(b a)))---> '((a b) (a a) (b b) (b a))
This isn't a map problem this is a foldr problem because you aren't modifying the list in place, you are condensing the list down into one item. That item happens to be a list but importantly that list can be smaller than what map can return. Also you can check if you should add or not in another if statement based on if the pair already exists in our accumulator.
This works if order doesn't matter (which I am assuming, you just want the set, let me know if this is not the case)
(define (Transitive-Closure L)
; Iterate over each pair (a b) in L,
(foldr (lambda (x transitive-pairs)
;Iterate over each pair (c d) in L,
(foldr (lambda (y sofar)
(let ([a (car x)]
[b (cadr x)]
[c (car y)]
[d (cadr y)])
;if b equal to c, and (a d) does not exist in L, it will add (a d) to L . Otherwise, return L.
(cond [(and (eq? b c) (not (member (list a d) L))) (cons (list a d) sofar)]
[(not (member x sofar)) (cons x sofar)]
[else sofar])))
transitive-pairs L)) '() L))
(check-expect (Transitive-Closure '((a b) (b c) (a c))) '((a b) (b c) (a c)))
(check-expect (Transitive-Closure '((a a) (b b) (c c))) '((a a) (b b) (c c)))
(check-expect (Transitive-Closure '((a b) (b a))) '((a b) (a a) (b b) (b a)))
(check-expect (Transitive-Closure '((a b) (b a) (a a))) '((a b) (b b) (b a) (a a)))
(check-expect (Transitive-Closure '((a b) (b a) (a a) (b b))) '((a b) (b a) (a a) (b b)))

Solving a functional programming problem using dr.racket

I am trying to implement a function called funPower, which takes a function f, an integer n and returns the function f^n. For example ((funPower sqrt 2) 16) should return 2, which is (sqrt (sqrt 16)).
This is what I have so far but it is not giving me correct output
(define (funPower f n)
(lambda(x) (if (<= n 1)
(f x)
(f (funPower f (- n 1)) x))))
First, you're missing one more pair of parens.
(define (funPower1 f n)
(lambda (x) (if (<= n 1)
(f x)
;; (f ( funPower1 f (- n 1)) x))))
(f ( ( funPower1 f (- n 1)) x)))) )
;; ^^^ ^^^
because (funPower1 f (- n 1)) returns a function to be called on x, the future argument value, as you show with the example, ((funPower sqrt 2) 16).
Second, it's <= 0, not <= 1, and the function f shouldn't be called at all in such a case:
(define (funPower2 f n)
(lambda (x) (if (<= n 0)
;; (f x) ^^^
x
(f ( ( funPower2 f (- n 1)) x)))) )
Now that it's working, we see that it defers the decisions to the final call time, of ((funPower f n) x). But it really could do all the decisions upfront -- the n is already known.
To achieve that, we need to swap the (lambda and the (funPower, to push the lambda "in". When we do, it'll become an additional argument to such augmented funPower:
(define (funPower3 f n)
(if (<= n 0) (lambda (x)
x )
(funPower3 f (- n 1) (lambda (x) (f x)))) )
Now this is completely out of sync. Where's that third argument?
(define (funPower4 f n fun)
(if (<= n 0) fun
(funPower4 f (- n 1) (lambda (x) (fun (f x)))) ))
That's a little bit better, but what's the fun, originally? Where does it come from? It must always be (lambda (x) x) at first or else it won't be right. The solution is to make it an internal definition and use that, supplying it the correct argument the first time we call it:
(define (funPower5 f n)
(define (loop n fun)
(if (<= n 0) fun
(loop (- n 1)
(lambda (x) (fun (f x))))))
(loop n (lambda (x) x)))
This kind of thing would normally be coded as a named let,
(define (funPower5 f n)
(let loop ((n n)
(fun (lambda (x) x)))
(if (<= n 0) fun
(loop (- n 1)
(lambda (x) (fun (f x)))))))
We could also try creating simpler functions in the simpler cases. For instance, we could return f itself if n is 1:
(define (funPower6 f n)
(cond
((zero? n) .....)
((= n 1) .....)
((< n 0) .....)
(else
(let loop ((n n)
(fun .....))
(if (= n .....) fun
(loop (- n 1)
(lambda (x) (fun (f x)))))))))
Complete it by filling in the blanks.
More substantive further improvement is to use exponentiation by repeated squaring -- both in constructing the resulting function, and to have it used by the function we construct!
try this:
(define funpow
(lambda (f n)
((lambda (s) (s s n (lambda (x) x)))
(lambda (s n o)
(if (zero? n)
o
(s s (- n 1)
(lambda (x)
(o (f x)))))))))
(define sqrt_2 (funpow sqrt 2))
(define pow2_2 (funpow (lambda (x) (* x x)) 2))
(sqrt_2 16)
(pow2_2 2)

What this functions in Scheme language do?

I'm a newbie and I didn't understand very well the language. Could anyone please explain to me what this functions do?
First function:
(define (x l)
(cond
((null? l) 0)
((list? (car l))
(+ (x (car l)) (x (cdr l))))
(else (+ 1 (x (cdr l))))
))
Second function:
(define (x l)
(cond
((null? l) 0)
((list? (car l))
(+ (x (car l)) (x (cdr l))))
(else (+ (car l) (x (cdr l)))
))
I do understand the begining but the conditions I didn't understand. Any help?
I will call your second function y.
Writing in pseudocode,
x [] -> 0
x [a . b] -> x a + x b , if list a
x [a . b] -> 1 + x b , else, i.e. if not (list a)
y [] -> 0
y [a . b] -> y a + y b , if list a
y [a . b] -> a + y b , else, i.e. if not (list a)
So for example,
x [2,3] = x [2 . [3]]
= 1 + x [3]
= 1 + x [3 . []]
= 1 + (1 + x [])
= 1 + (1 + 0 )
and
y [2,3] = y [2 . [3]]
= 2 + y [3]
= 2 + y [3 . []]
= 2 + ( 3 + y [])
= 2 + ( 3 + 0 )
See? The first counts something in the argument list, the second sums them up.
Of course both functions could be called with some non-list, but then both would just cause an error trying to get (car l) in the second clause, (list? (car l)).
You might have noticed that the two are almost identical. They both accumulates (fold) over a tree. Both of them will evaluate to 0 on the empty tree and both of them will sum the result of the same procedure on the car and cdr when the car is a list?. The two differ when the car is not a list and in the first it adds 1 for each element in the other it uses the element itself in the addition. It's possible to write the same a little more compact like this:
(define (sum l)
(cond
((null? l) 0) ; null-value
((not (pair? l)) l) ; term
(else (+ (sum (car l)) (sum (cdr l)))))) ; combine
Here is a generalisation:
(define (accumulate-tree tree term combiner null-value)
(let rec ((tree tree))
(cond ((null? tree) null-value)
((not (pair? tree)) (term tree))
(else (combiner (rec (car tree))
(rec (cdr tree)))))))
You can make both of your procedures in terms of accumulate-tree:
(define (count tree)
(accumulate-tree tree (lambda (x) 1) + 0))
(define (sum tree)
(accumulate-tree tree (lambda (x) x) + 0))
Of course you can make a lot more than this with accumulate-tree. It doesn't have to turn into an atomic value.
(define (double tree)
(accumulate-tree tree (lambda (x) (* 2 x)) cons '()))
(double '(1 2 ((3 4) 2 3) 4 5)) ; ==> (2 4 ((6 8) 4 6) 8 10)

A helpfunction as parameter?

I dont know how to fix one of my problems.
I have programmed two helpfunction for my main function but it wont work.
;;Main function
(define (FunctionA a b c)
(/(-(* -1 b) VariableD)aNotNull))
(check-expect (FunctionA 1 1 1)-1)
;;Helpfunction1:
(define (VariableD a b c)
(if (> 0(-(* b b)(* 4 (* a c)))) (error "No negative numbers allowed")
(sqrt(-(* b b)(* 4 (* a c))))))
(check-expect (VariableD 0 0 0) 0)
(check-error (VariableD 1 2 3) "No negative numbers allowed")
;helpfunction2:
(define (aNotNull a)
(if (= 0 (* 2 a)) (error "Zerodivisor not allowed")
(* 2 a)))
(check-error (aNotNull 0 ) "Zerodivisor not allowed")
(check-expect(aNotNull 2) 4)
I get the error: expects a number as 2nd argument, given (lambda (a1 a2 a3) ...)
But I dont know how to fix it.
Hope you can help me out :)
(define (FunctionA a b c)
(/ (- (* -1 b) VariableD) aNotNull))
VariableD here refers to the function you defined. You probably want to instead call the function with the a, b and c inputs from FunctionA, and use the result in FunctionA.
The same goes for your usage of aNotNull. I'm just inferring that these are the variables you'll want to pass to these functions in this revised FunctionA:
(define (FunctionA a b c)
(/ (- (* -1 b)
(VariableD a b c))
(aNotNull a)))

Scheme recursive unzip function for more than one list

I need help finishing up an unzip function which takes a zipped list and returns a list of two lists. The result I want is as follows...
(unzip '((a b) (1 2)))
'((a 1) (b 2))
(unzip '((a 1) (b 2) (c 3)))
'((a b c) (1 2 3))
(unzip '(unzip '()))
'(() ())
I can get my code to work for the null case and with a list containing two lists, but I'm having a hard time figuring out how to make it recursive and work for more than 2 lists such as the second example.
(define (unzip l)
(if (null? l)
'(() ())
(map list (car l) (car (cdr l)))))
This will work fine for an empty list or two lists, but I have a hard time setting up the recursive part to work with three or possibly more lists.
This is a pretty standard operation, it's equivalent to finding the transpose of a list of lists. It's usually implemented like this:
(define (unzip lst)
(apply map list lst))
It'll work for the first two examples. The third, I believe is ill-defined, but if you want to make it work for that weird case, I'll leave that as an exercise ;)
Notice that if you unzip an unzipped list you get back the original input ... meaning that unzip is also zip!
(unzip '((a b) (1 2) (x y)))
=> '((a 1 x) (b 2 y))
(unzip '((a 1 x) (b 2 y)))
=> '((a b) (1 2) (x y))
(apply map list '((a 1) (b 2) (c 3) (d 4) (e 5)))
;Value 16: ((a b c d e) (1 2 3 4 5))
this does it. BTW the same trick does it for zipping as well, provided all the lists are of the same length:
(apply map list (list '(a b c d e) '(1 2 3 4 5)))
;Value 17: ((a 1) (b 2) (c 3) (d 4) (e 5))
Are you aware that in Scheme a function can return multiple values?
(define (unzip list)
(values (map car list)
(map cadr list)))
and then use it like this:
(let-values ([(z1 z2) (unzip '((a 1) (b 2)))])
;; Use z1 and z2
...)