使用集合作为函数 [英] Working with Sets as Functions

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

问题描述

来自 FP 课程:

  type Set = Int =>布尔值//谓词

/ **
*表示一个集合是否包含给定的元素。
* /
def包含(s:Set,elem:Int):Boolean = s(elem)

为什么会有意义?

  assert(contains(x => true,100)) 

基本上它提供的值 100 到函数 x =>真。即,我们提供100,它返回 true

但是这与set有什么关系?无论我们放什么,它都会返回 true

我知道我们可以提供我们自己的集合实现/函数作为一个参数,它可以表示提供的值在集合内部的事实或者不) - 然后(仅)这个实现使得包含函数被某些意义/含义/逻辑/功能填充。


但到目前为止,它看起来像一个无意义的函数。它被命名为包含,但名称不代表逻辑。我们可以将它称为 apply(),因为它的作用是将一个函数(第一个参数)应用于一个值(第二个参数)。只有名称包含可能会告诉读者作者可能想说的内容。是不是太抽象了,也许呢?

在上面显示的代码片段中,任何集合<$​​ c $ c> >

解决方案 S 由所谓的特征函数表示,即给定某个整数 i 的函数检查是否 i S 中是否包含。因此,您可以想象出这样一个特征函数 f ,比如它是一个集合,即


{ i |所有 i 其中 fi }


如果您想到任何类型为 Int =>的函数,布尔作为集合(它由类型同义词 Set = Int =>布尔>表示),那么您可以描述 contains


给定一个 f 和一个整数 i contains(f,i)检查是否 i f 或不是。


一些示例集可能会使想法更明显:

 设置特征函数
空集x =>假
通用集x => true
奇数组x => x%2 == 1
偶数组x => x%2 == 0
小于10的数字的集合x => x < 10

示例:集合{1,2​​,3}可以由

  val S表示:Set =(x => 0 <= x&& x <= 3 )

如果您想知道某些数字 n 包含(S,n)

  

但是当然(就像您已经观察过的那样),您会直接得到相同的结果

  S(n)

虽然这个更短,前者可能更易于阅读(因为其意图显而易见)。


From a FP course:

type Set = Int => Boolean  // Predicate

  /**
   * Indicates whether a set contains a given element.
   */
  def contains(s: Set, elem: Int): Boolean = s(elem)

Why would that make sense?

assert(contains(x => true, 100))

Basically what it does is provide the value 100 to the function x => true. I.e., we provide 100, it returns true.

But how is this related to sets?

Whatever we put, it returns true. Where is the sense of it?

I understand that we can provide our own set implementation/function as a parameter that would represent the fact that provided value is inside a set (or not) - then (only) this implementation makes the contains function be filled by some sense/meaning/logic/functionality.

But so far it looks like a nonsense function. It is named contains but the name does not represent the logic. We could call it apply() because what it does is to apply a function (the 1st argument) to a value (the 2nd argument). Having only the name contains may tell to a reader what an author might want to say. Isn't it too abstract, maybe?

解决方案

In the code snippet you show above, any set S is represented by what is called its characteristic function, i.e., a function that given some integer i checks whether i is contained in the set S or not. Thus you can think of such a characteristic function f like it was a set, namely

{i | all integers i for which f i is true}

If you think of any function with type Int => Boolean as set (which is indicated by the type synonym Set = Int => Boolean) then you could describe contains by

Given a set f and an integer i, contains(f, i) checks whether i is an element of f or not.

Some example sets might make the idea more obvious:

Set                                Characeristic Function
 empty set                          x => false
 universal set                      x => true
 set of odd numbers                 x => x % 2 == 1
 set of even numbers                x => x % 2 == 0
 set of numbeers smaller than 10    x => x < 10

Example: The set {1, 2, 3} can be represented by

val S: Set = (x => 0 <= x && x <= 3)

If you want to know whether some number n is in this set you could do

contains(S, n)

but of course (as you already observed yourself) you would get the same result by directly doing

S(n)

While this is shorter, the former is maybe easier to read (since the intention is somewhat obvious).

这篇关于使用集合作为函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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