原子的 ruby 方法?计划中
我正在尝试使用 Ruby 进行“The Little Scheduler”中的练习。我想知道atom的等效方法?在 Ruby 方案中。
原子?是一个方法,如果传入的参数是原子,则返回 true,否则返回 false。 据我了解,原子可以是一串字符或一个数字。它不可能是一个列表。
I'm trying to do the exercises in "The Little Schemer" using Ruby. I'd like to know the equivalent method of atom? in Scheme for Ruby.
atom? is a method that returns true if the argument passed in is an atom and returns false otherwise.
From what I understand, an atom can be a string of characters or a number. It can't be a list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Scheme 中的原子,由 The Little Schemer 定义:
因此,Scheme 中的原子是除了列表(使用 'cons' 运算符创建的东西)和 null 之外的所有内容。在Scheme中,最终是符号、数字、方法,可能还有一些我忘记的东西。 Ruby 要复杂得多。
因此,出于 The Little Scheduler 中练习的目的,Ruby 中的原子是 FixNum、Symbol、TrueValue 或 FalseValue。或者说是一个方法。然后对于列表,您需要为 Ruby 的数组类型定义 cons、car 和 cdr 方法。我实际上可能会说,任何不是数组且不是 nil 的东西都是原子。这种讨论在某些时候会变得主观。
然后你可能需要做更多的黑客工作才能让一切都表现得一样。老实说,虽然我很喜欢《Lisp》,但它确实有一些 Lisp 特有的东西,而且我认为,虽然我可以将它改编成另一种函数式语言,比如 Haskell,而不需要太多麻烦,但我认为让它在像 Ruby 这样的语言中工作会带来更多的麻烦,而不是值得的。
Atoms in Scheme, as defined by The Little Schemer:
So atoms in Scheme are everything except lists (things created with the 'cons' operator) and null. In Scheme, that ends up being symbols, numbers, methods, and probably a few more things I'm forgetting. Ruby's a lot more complicated.
So for the purposes of doing is the exercises in The Little Schemer, an atom in Ruby is a FixNum, a Symbol, a TrueValue, or a FalseValue. Or a method. And then for lists, you're going to need to define cons, car, and cdr methods for Ruby's array type. I might actually say that anything that's not an Array and is not nil is an atom. This discussion gets subjective at some point.
And then you're probably going to need to do a good bit more hacking to get everything to behave the same. Quite honestly, while I love The Little Schemer, it really does have some things that are fairly Lisp-specific, and while I can see adapting it to another functional language like Haskell without too much trouble, I think it's going to be more trouble than it's worth to get it to work in a language like Ruby.
我猜想 Ruby 中与原子最接近的等价物是文字 - Fixnum、布尔值、符号和 nil。这实际上归结为实现细节,但这些对象直接由堆栈上的值而不是 C 指针表示。那么,#atom?可能看起来像这样:
I guess the closest equivalent to atoms that Ruby has are the literals - Fixnum, booleans, symbols, and nil. This effectively comes down to an implementation detail, but those objects are represented directly by a value on the stack rather than as a C pointer. So, #atom? might look like this:
按照惯例,类似序列的对象响应“to_a”(到数组)方法。
一个可能的原子实现可能是:
By convention, sequence-like objects respond to the 'to_a' (to array) method.
A possible atom implementation could be: