Odd Number of SiCp-Qiu

Source: Internet
Author: User
[Go to] http://somethin.is-programmer.com/from SiCExercise 2.6:

In a language that can perform various operations on the process, we can have no number at all (at least when we only consider non-negative integers). We can add 0 and add one operation as follows:

(define zero (lambda (f) (lambda (x) x)))(define (add-1 n)  (λ(f)    (λ(x)      (f ((n f) x)))))

This expression is called the church count. Its name comes from Alonzo Church, the inventor of Alonzo, And the lambda algorithm was invented by him.

Define One and Two directly (without zero and add-1) (Note: Use replacement to calculate (add-1 zero )). please provide a direct definition of the addition Process + (do not apply add-1 repeatedly)

Before doing this, let's take a look at other interesting implementations using the λ operator.

1. Boolean Value

(define true   (λ (x)     (λ (y) x)))(define false  (λ (x)     (λ (y) y)))(define if   (λ (v)     (λ (t)       (λ (f) ((v t) f)))))

True is a function that accepts parameter X. This function returns a function that accepts Parameter Y. This function returns the parameter X that is accepted by true.

Now we want (if true 1 0) to return 1 to see if this is the case. The reduction process is as follows:

(((if true)1)0)((((λ (v)     (λ (t)       (λ (f)         ((v t) f)))) (λ (x)     (λ (y) x)))1)0)(((λ (t)     (λ (f)       (((λ (x)           (λ (y) x)) t) f)))1)0)(((λ (x)     (λ (y) x))  1) 0) ((λ (y) 1)0)  1

Application (λ (V) (λ (t) (λ (f) (v t) F) = (λ (v t f) (v t) f ))

If can be abbreviated:

(define if  (λ (v t f)    ((v t) f)))

In this way, you can directly use (if true 1 0) instead of (if true) 1) 0.

2. indicates the order pair.

Use the process to representSiC. Here is another version.

(define mkpair  (λ (x)    (λ (y)      (λ (s)        ((s x) y)))))(define fst  (λ(p)    (p true)))(define snd   (λ(p)    (p false)))

Call:

> (fst ((mkpair 1) 2))1

3. Now let's seeOdd Qiu number

Zero and add-1 have been defined, so we hope (add-1 zero) to get the first definition of one:

(add-1 zero)(add-1 (λ(f)         (λ(x) x)));α relation:;different name means nothing((λ(n)   (λ(f)     (λ(x)       (f ((n f) x))))) (λ(b)   (λ(a) a)))(λ(f)  (λ(x)    (f      (((λ(b)         (λ(a) a)) f) x))))(λ(f)  (λ(x)    (f      ((λ(a) a) x))))(λ(f)  (λ(x)    (f x)))

The definition of one is:

(define one  (λ(f)    (λ(x)      (f x))))

Similarly, the definition of two is:

(define two  (λ(f)    (λ (x)      (f (f x)))))

It can be seen that.Odd Qiu numberThe number N in is a high-order function with a function F as the parameter. It returns a function, which has a parameter X, which takes f over X n times.

Therefore, add accepts twoOdd Qiu numberN and M. ReturnedOdd Qiu numberN + M is a function that acts on N + m times on X. Therefore, add can be defined as follows:

(define add  (λ(m)    (λ(n)      (λ(f)        (λ(x)          ???)))))

Now, let's take a look at the added-1 defined in the question:

(define (add-1 n)  (λ(f)    (λ(x)      (f ((n f) x)))))

What is the result of (n f) x? Recall that N is defined to apply F to x n times, then (n f) x) obtains the value after F is applied to x n times.

Then (f (n f) X) is to function f once, and F and X are unknown because they are function parameters. so we get the definition of N + 1.

Then the question mark in "add" should be the value after f acts on X n times. Then, this value is treated as X and it acts on m times F. Therefore, the definition of "add" is as follows:

(define add  (λ(m)    (λ(n)      (λ(f)        (λ(x)          ((m f)           ((n f) x)))))))

Verify:

> ((((add one) two) add1) 4)7

Correct ~

We can get the definitions of multiplication mult and sub-1. mult accepts M and N, uses (n f) as the new F, and passes in M. sub-1 determines whether N is 0 first. If yes, 0 is returned; otherwise, N-1 is returned.

(define mult  (λ(m)    (λ(n)      (λ(f)        (m(n f))))))(define sub1  (λ(n)    (λ(f)      (λ(x)        (((n (λ(g)               (λ(h)                 (h (g f)))))         (λ(u)           x))         (λ(u)           u))))))

Refer:

  • SiC
  • Programming Language and Lambda calculi
  • Odd Qiu number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.