用lambda函数表示集合 [英] representing sets with lambda functions

查看:154
本文介绍了用lambda函数表示集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解我真的需要做什么,并希望得到一些外部的意见或指向一个很好的参考。我被要求使用程序表示法来实现数字集合。每个集合将是一个参数函数,它接受一个数字并决定该数字是否在集合中。我必须创建一些函数(我已经阅读过,可以在一行中定义):


  1. 函数返回一个函数数字作为参数并检查数字是否在集合中。

  2. 返回arg1或arg2中所有元素集合的联合函数

  3. 上面的交集函数
  4. 函数返回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:

  1. A function that returns a function taking a number as an argument and checks if the number is in the set.
  2. A union function that returns the set of all elements in arg1 or arg2
  3. Intersection function of above
  4. Function that returns elements in arg1 but not arg2
  5. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆