我请您参考以下短代码:
('a 1)
;==> nil
('a 1 2)
;=>2
为什么a
在这种情况下可以将符号用作功能?
它允许您以一种不错的语法查找以该符号为键的地图。
下面是等效的:
('a my-hash-map)
(get my-hash-map 'a) ; returns value associated with 'a or nil if not present
为了进一步的方便,您还可以提供默认值作为可选的第二个参数:
('a my-hash-map 42) ; returns 42 if my-hash-map does't have the key 'a
就您的示例而言,('a 1)返回nil,因为(get 1'a )返回nil,而('a 1 2)返回提供的默认值2。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句