Custom eqv?/equal? function in scheme

爷,独闯天下 提交于 2020-06-01 06:29:26

问题


How would I go about writing my own eqv? or equal? in scheme? Would I just do a cond and look for symbol?, number?, etc and return the appropriate #t or #f?


回答1:


As per R5RS, the minimum specifications for an implementation of eqv? (when passed two arguments obj1 and obj2) to evaluate to #tare:

  • obj1 and obj2 are both #t or both #f. (how two boolean literals evaluate to the same value is implementation dependent).
  • obj1 and obj2 are both symbols and

(string=? (symbol->string obj1) (symbol->string obj2)) =) => #t

  • obj1 and obj2 are both numbers, are numerically equal (=), and are either both exact or both inexact.
  • obj1 and obj2 are both characters and are the same character according to the char=? procedure.
  • both obj1 and obj2 are the empty list.
  • obj1 and obj2 are pairs, vectors, or strings that denote the same locations in the store (See section 3.4 of R5RS).
  • obj1 and obj2 are procedures whose location tags are equal (A lambda expression is conceptually tagged with a storage location. What that means varies between Scheme implementations. Also see section 4.1.4 of R5RS).

equal? could be implemented in terms of eqv? as it recursively compares the contents of pairs, vectors, and strings, applying eqv? on other objects such as numbers and symbols.



来源:https://stackoverflow.com/questions/4373714/custom-eqv-equal-function-in-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!