How to apply an anonymous function from a list in a schema?

I am studying the circuit. What is wrong with the code below? I want to write a program that takes the first function from a list and then applies it to a number?

(define num 3) ;;I want to do something like this which returns 3 ((Ξ» (x) x)num) ;;but my functions are in a list so this should return3 ((first '((Ξ» (x) x) (Ξ» (x) (* xx)))) num) 

I need this error for the above code:
application procedure: the expected procedure if: (Ξ» (x) x); arguments were: 3

What does it mean when I get this output?

When I do not apply anything, I get a good result.

 (first '((Ξ»(x) x)(Ξ»(x) (*xx)))) 

returns (Ξ» (x) x)

+6
scheme
source share
3 answers

You quote "lambda", therefore it is not evaluated.

If you just load (Ξ» (x) x) at the prompt, DrScheme shows you #<procedure> , which means it really appreciated the lambda and gave you a close. By quoting it, you give the Schema only a list of characters.

If you want to put your functions in a list, you can do:

 ((first (list (lambda (x) x) (lambda (x) (* xx)))) num) 

The quote allows you to create a list, yes, but one whose content is not rated. The list function creates a list of all its arguments after they have been evaluated.

You can also quasicopy the list if you want:

 ((first `(,(lambda (x) x) ,(lambda (x) (* xx)))) num) 
+11
source share

What is the difference between these expressions?

 > (procedure? (lambda (n) n)) #t > (procedure? (quote (lambda (n) n))) #f > (procedure? '(lambda (n) n)) #f 

Jay answered that, but I still can’t promote it.

+2
source share

(lambda (x) x) is not a procedure. This is the form that evaluates the procedure. People are a little free of terminology and often call the lambda form a procedure as a kind of shorthand. "Ceci n'est pas une pipe".

+2
source share

All Articles