How to use lambda in DrRacket schema

I would like to ask if you can help me with programming. I am trying to make a lambda expression of this form:

λz.x(yz) 

The way I understand this is that y is a function applied to the value of z . Then x is the function applied to what appears if the function y is applied to z . This expression says the following:

 λz.x(yz) means: Do the following with the argument z: 
  • Apply the y function to z .
  • Apply function x to what comes out of the first procedure.

I made this program to try to get Scheme to do all of the above:

 (define (zlamb) (lambda (z) (lambda (x) (* (lambda (y) (* z 4)) 2)))) 

When I run it, all I get is:

 Welcome to DrRacket, version 5.3 [3m]. Language: R5RS; memory limit: 128 MB. ( (zlamb) 3) procedure:...lambdaefing1.rkt:3:4 > 

Can someone explain to me what I'm doing wrong? I wanted to get (3 * 4) * 2 = 24 . So I made (or thought I created) an inner function y = z * 4 and an outer function x = y(z) * 2 .

I searched all over the internet for explanations, but can't find the specific needle I'm looking for in a haystack.

+4
source share
2 answers

Let your program break down from top to bottom:

 (* z 4) 

Multiply z by 4

 (lambda (y) (* z 4)) 

Return Function z*4

 (* (lambda (y) (* z 4)) 2) 

The product of this function and 2. You cannot multiply the function by 2. This probably causes your error; perhaps you want to do the following:

 (define (zlamb) (lambda (z) ((lambda (y) (* 2 (yz))) ; Note the two parenthesis before lambda - this is a function application (lambda (z2) (* z2 4))))) 

First, note that both z same, since z2 tied to the value of z on line 3. They can be referred to as z , but I named them differently to prevent confusion.

It then appears that your main problem is confusing the name of the function with its arguments:

 (lambda (name) ...) 

creates an anonymous function with argument name . The reason we can refer to the anonymous function in line 4 as y in line 3 is to make a construct

 ((lambda (y) ...) (lambda ...)) 

which passes the second function as an argument to the first, thereby calling it y .

+1
source

Everything that Arafinwe says makes sense to me. However, I am still worried that you may not understand your assignment.

More specifically, lambda calculus is essentially a subset of schematic terms with very slightly different syntax. In particular, the term lambda calculus λz. written in the diagram as (lambda (z)). Well, with the caveat, that also needs to be translated.

The application (zx) is written in the diagram simply as (zx). In addition, mathematicians are lazy, and they are sometimes left without pairs, so a (bc) is actually a shorthand for (a (bc)). I try my best not to translate my term right here :).

Please note, however, that translating your current lambda calculus term into a schema will not be a well-formed program, as it contains free links ("unrelated variables") y and z.

+2
source

Source: https://habr.com/ru/post/1435170/


All Articles