用lambda函数表示集合 [英] representing sets with lambda functions
问题描述
- 函数返回一个函数数字作为参数并检查数字是否在集合中。
- 返回arg1或arg2中所有元素集合的联合函数
- 上面的交集函数
- 函数返回arg1中的元素但不是arg2
- 等
现在我知道这只是简单地包含一个lambda函数,但是我想我对如何表示一个集合并在lambda函数中检查它感到困惑?如果有人能指引我正确的方向,我将不胜感激。
您必须认识到,您的TODO列表中的第一个函数是构造函数。其他人从我认为的地方出来。
(define(make-set x)(lambda(y)(eq?xy )))
(define(intersection xy)(lambda(z)(or(xz)(yz))))
(define(intersection xy)(lambda(z)(and(xz)( y)))))
(define(difference xy)(lambda(z)(and(xz)(not(yz)))))
(define set-5(make- (set-5 4)
(set-5 5)
(定义set-45(union-make-set 4)(make-set 5) )))
(set-45 3)
(set-45 5)
(定义集合-34(union-make-set 3)(make-set 4) ))
(define set-4(intersection set-34 set-45))
(set-4 3)
(set-4 5)
(set-3 4)
(set-3 5)
$ b(定义set-3(差异设置-34 set-45))
(set-3 4) $ b(set-3 3)
I am struggling with understanding what I really need to do, and would like some outside input or a point to a good reference. I have been asked to use procedural representation to "implement sets of numbers." Each set will be a one argument function that takes a number and decides if the number is in the set. A few functions (that I have read can be defined in one line) that I have to create:
- A function that returns a function taking a number as an argument and checks if the number is in the set.
- A union function that returns the set of all elements in arg1 or arg2
- Intersection function of above
- Function that returns elements in arg1 but not arg2
- etc.
Now I know this is simply enclosing a lambda function, but I guess I am confused on how to represent a set and check it within the lambda function? If someone could point me in the right direction I would appreciate it.
You have to realise that the first function in your TODO list is the constructor. The rest falls out from there I think.
(define (make-set x) (lambda (y) (eq? x y)))
(define (union x y) (lambda (z) (or (x z) (y z))))
(define (intersection x y) (lambda (z) (and (x z) (y z))))
(define (difference x y) (lambda (z) (and (x z) (not (y z)))))
(define set-5 (make-set 5))
(set-5 4)
(set-5 5)
(define set-45 (union (make-set 4) (make-set 5)))
(set-45 3)
(set-45 5)
(define set-34 (union (make-set 3) (make-set 4)))
(define set-4 (intersection set-34 set-45))
(set-4 3)
(set-4 5)
(set-4 4)
(define set-3 (difference set-34 set-45))
(set-3 4)
(set-3 5)
(set-3 3)
这篇关于用lambda函数表示集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!