lisp:if语句的格式正确 [英] lisp: properly form if statement

查看:579
本文介绍了lisp:if语句的格式正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我自己学习普通口语的一项任务,我正在尝试重新创建 lodash

As a task for myself to learn common lisp, I'm trying to recreate lodash.

在重新创建_.chunk的过程中,我编写了以下代码来测试可选参数

En route to recreating _.chunk, I've written the following to test for an optional argument

(defun _.chunk (array &optional size)
    (if (size)
        (write ("there") )
        (write ("not") )
    ) 
)  

设置(setf x #('a 'b 'c 'd))然后运行(_.chunk x),我得到一个错误:

Setting (setf x #('a 'b 'c 'd)) and then running (_.chunk x), I get an error:

; caught ERROR:
;   illegal function call

;     (SB-INT:NAMED-LAMBDA _.CHUNK
;         (ARRAY &OPTIONAL SIZE)
;       (BLOCK _.CHUNK
;         (IF (SIZE)
;             (WRITE ("there"))
;             (WRITE ("not"))))) 

测试可选功能参数的正确方法是什么?

What's the correct way to test for optional function parameters?

推荐答案

size-p ,可以在关键字或可选参数的默认值之后指定的可选变量的名称如果参数作为参数传递给函数调用,则为true.

size-p, the name of the optional variable which may be specified after the default value of a keyword or optional argument, will be true if the parameter was passed as an argument to a function call.

因此您可以执行以下操作:

so you could do something such as:

(defun _.chuck (array &optional (size 0 size-p))
  (if size-p
      (rest of your form...)))

这篇关于lisp:if语句的格式正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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