How do you evaluate exponentiation of church numerals? - lambda-calculus

Exponentiation over church numerals is defined as:
expt ≡ λmnsz.nmsz
But I'm having some trouble evaluating it in cases where the power is not 0 or 1. Consider this example:
expt C3 C2 ≡ [λmnsz.nmsz](λsz.s^3 z) (λsz.s^2 z)
where
λsz.s^2 z = λsz.s(sz)
and Cn represent Church Numeral n
Substituting for m and n, I get:
λsz. (λsz.s^2 z)(λsz. s^3 z)sz
λsz. (λsz.s^2 z)(s^3 z)
λsz. (s^3)^2 z
And by the fact that
λsz. (s^m)^n z = s^(m*n) z
the last statement is reduced to
C6 ≡ λsz. s^6 z
but expt C3 C2 should evaluate to C9.
So where did I go wrong?

This is wrong:
λsz. (λsz.s^2 z)(λsz. s^3 z)sz
λsz. (λsz.s^2 z)(s^3 z)
λsz. (s^3)^2 z
You cannot apply (λsz. s^3 z) to sz. You have to apply (λsz.s^2 z) to (λsz. s^3 z), because evaluation is from left to right.
λsz. (λsz.s^2 z) (λsz. s^3 z) sz
λsz. (λz.(λsz. s^3 z)^2 z) sz
λsz. (λz.(λsz. s^3 z)((λsz. s^3 z) z)) sz
λsz. (λz.(λsx. s^3 x)((λsy. s^3 y) z)) sz
λsz. (λz.(λsx. s^3 x) (λy. z^3 y)) sz
λsz. (λz.(λx. (λy. z^3 y)^3 x)) sz
λsz. (λz.(λx. (λy. z^3 y)((λy. z^3 y) ((λy. z^3 y) x)))) sz
λsz. (λz.(λx. (λy. z^3 y)((λy. z^3 y) (z^3 x)))) sz
λsz. (λz.(λx. (λy. z^3 y)(z^3 (z^3 x)))) sz
λsz. (λz.(λx. (λy. z^3 y)(z^6 x))) sz
λsz. (λz.(λx. (z^3 (z^6 x)))) sz
λsz. (λz.(λx. (z^9 x))) sz
λsz. (λx. (s^9 x)) z
λsz. (s^9 z)

Related

SICP 1.3 interpreter error unknown identifier: and

I am using the interpreter from browser (without any local setup): https://inst.eecs.berkeley.edu/~cs61a/fa14/assets/interpreter/scheme.html
and getting the following interpreter exception message:
SchemeError: unknown identifier: and
Current Eval Stack:
-------------------------
0: and
1: (cond (and (< x y) (< x z)) (sqrt-sum y z))
2: (f 1 2 3)
for the following code:
; define a procedure that takes three numbers
; as arguments and returns the sum of the squares
; of the two larger numbers
(define (square) (* x x))
(define (sqrt-sum x y)
(+ (square x) (square y)))
(define (f x y z)
(cond (and (< x y) (< x z)) (sqrt-sum y z))
(cond (and (< y x) (< y z)) (sqrt-sum x z))
(cond (and (< z y) (< z x)) (sqrt-sum x y)))
(f 1 2 3)
I am struggling to find any info about specific Scheme version this interpreter is based on; sorry
That's not the correct syntax for cond. The syntax is
(cond (condition1 value1...)
(condition2 value2...)
...)
In your code the first condition should be the expression (and (< x y) (< x z)). But you don't have the parentheses around the condition and value. You have just and where the condition should be, not (and (< x y) (< x z)). Since and isn't a variable with a value, you get an error because of that.
The correct syntax is:
(define (f x y z)
(cond ((and (< x y) (< x z)) (sqrt-sum y z))
((and (< y x) (< y z)) (sqrt-sum x z))
((and (< z y) (< z x)) (sqrt-sum x y))))

How would one prove ((p ⇒ q) ⇒ p) ⇒ p, using the Fitch system

FYI, the logic program I use cannot do contradiction introductions. This point is most likely irrelevant, for I highly doubt I would need to use any form of contradiction for this proof.
In my attempt to solve this, I started off with assuming (p ⇒ q) ⇒ p)
Is this correct?
If so, what next? Forgive me if the solution seems so obvious.
(p ⇒ q) ⇒ p
((p ⇒ q) ⇒ p) ∨ (p ⇒ p) ; (X ⇒ X) and Or introduction
((p ⇒ q) ∨ p) ⇒ p ; (X ⇒ Z) ∨ (Y ⇒ Z) |- (X ∨ Y ⇒ Z)
((¬p ∨ q) ∨ p) ⇒ p ; (p ⇒ q) ⇔ (¬p ∨ q)
((¬p ∨ p) ∨ q) ⇒ p ; (X ∨ Y) ∨ Z |- (X ∨ Z) ∨ Y
(true ∨ q) ⇒ p ; (¬X ∨ X) ⇔ true
true ⇒ p ; (true ∨ X) ⇔ true
p ; Implication elimination
((p ⇒ q) ⇒ p) ⇒ p ; Implication introduction

calling a subroutine in scheme

I'm trying to call a subroutine from a function on scheme this what i have so far.
(define (main)
(display "Ingrese un parametro: ")
(define x (read-line))
(newline)
(display "Ingrese una posicion: ")
(define dig (read))
(newline)
(if (string? x)
(begin
(if (number? dig)
(begin
;(vababa x dig))) <---calling subrutine
this is the subroutine that im trying to call
define (vababa x1 dig1)
(define a (string-pad-right x dig1))
(define b (string-pad-left x dig1))
(define z (string-append a b))
z
)
but its not returning nothing.. please tell me what im doing wrong.
In vababa you are using a unbound variable x. Perhaps you meant x1?
It's not allowed to use define in procedure bodies that evaluates to values based on previous ones.
(define (vababa x1 dig1)
(define a (string-pad-right x dig1)) ; x does not exist anywhere
(define b (string-pad-left x dig1)) ; x does not exist anywhere
(define z (string-append a b)) ; a and b does not exist yet
z) ; now a and b exists but you won't get there since `string-append` didn't get string arguments
You can do one of:
(define (vababa x1 dig1)
(string-append (string-pad-right x1 dig1) ; replaced x with x1
(string-pad-left x1 dig1))) ; replaced x with x1
(define (vababa x1 dig1)
(define a (string-pad-right x1 dig1)) ; replaced x with x1
(define b (string-pad-left x1 dig1)) ; replaced x with x1
;; in the body a and b exists
(string-append a b))
(define (vababa x1 dig1)
(let ((a (string-pad-right x1 dig1)) ; replaced x with x1
(b (string-pad-left x1 dig1))) ; replaced x with x1
(string-append a b)))
(define (vababa x1 dig1)
(let* ((a (string-pad-right x1 dig1)) ; replaced x with x1
(b (string-pad-left x1 dig1)) ; replaced x with x1
(z (string-append a b)))
z))

Scheme The object is not applicable

(define (min2 a b)
(if (> a b)
b
a))
(define (min3 x y z)
(min2 (min2 x y) (min2 y z)))
(define (sum x y z)
(- (+ x y z) min3(x y z)))
(sum 1 2 3)
error tip: ;The object 1 is not applicable.
Scheme Newbie learn Scheme, So I don't understand why (min3 1 2 3) is right, but (sum 1 2 3) is wrong?
You had min3 in the wrong place in the definition of sum:
(define (sum x y z)
(- (+ x y z) (min3 x y z)))

Elegant code for binary addition?

I just wrote the function add-registers for binary addition of two n-bit registers in Racket (using bit-add function as a helper):
(define (bit-add x y c)
(values (bitwise-xor x y c) (bitwise-ior (bitwise-and x y)
(bitwise-and x c)
(bitwise-and y c))))
(define (add-registers xs ys)
(let ([carry 0])
(values (reverse (for/list ([b1 (reverse xs)] [b2 (reverse ys)])
(let-values ([(nb nc) (bit-add b1 b2 carry)])
(set! carry nc)
nb)))
carry)))
But I found my code pretty ugly. So I wonder if this could be written more concisely and elegant?
Here's a new version of add-registers that looks somewhat nicer:
(define (add-registers xs ys)
(for/fold ([carry 0] [bs empty])
([b1 (reverse xs)] [b2 (reverse ys)])
(define-values (nb nc) (bit-add b1 b2 carry))
(values nc (cons nb bs))))

Resources