连续调用/评估形式? [英] Consecutive calls/evaluations in a form?
问题描述
使用XLISP编写程序,但我似乎遇到了一个简单的基本问题,我可以' t似乎工作:也许有人有一个快速修复。
我试图写一个if语句谁是then-clause评估多个表单并返回
例如:
(setq POSITION'DINING- ROOM)
(defun LOOK(DIRECTION ROOM)...)
(defun SETPOS(ROOM)...)
(defun WHERE()...)
(defun MOVE(DIRECTION)
(if(not(equal nil(LOOK DIRECTION POSITION)));如果在该方向上有空间
(; Then-block:Go
(SETPOS(LOOK DIRECTION ROOM))
(WHERE)
)
(; Else-block:返回错误
( list'CANT'GO'THERE)
)
)
逻辑等价打算:
函数移动(方向)
{
$ b $ if(Look(Direction,Room)!= null)
{
SetPos(Look(Direction,Room));
return Where();
}
else
{
returnCan not go there;
$ b $ / code
$ b )
我遇到的问题是:
(
(SETPOS(LOOK DIRECTION ROOM))
(WHERE)
)
我只想返回WHERE的评估,但我需要先执行SETPOS函数。 XLISP不喜欢额外的括号:如果我删除外部集合,我的WHERE列表就变成了我的其他(我不想那样)。如果我删除了SETPOS和WHERE的集合,它将WHERE视为SETPOS的参数;我也不想这样做。
那么,我该如何评估第一个,然后是第二个,然后返回最后评估的值?
解决方案 Lisp通常提供类似PROGN的功能。
(progn
(prog
( do-this)
(do-that))
再看看你的代码:
(if(not(equal nil(LOOK DIRECTION POSITION)))
(等于零(FOO ))与(NULL FOO)
(NOT(NULL FOO))是相同的FOO。
所以你可以简单地写:
(if(LOOK DIRECTION POSITION)... ...)
或者如果您想检查是否有房间:
(if(ROOM-P(LOOK DIRECTION POSITION))...)
ROOM-P将是一个谓词,如果某个房间是某个东西,它将返回T.
还希望使用典型的Lisp缩进:
(defun MOVE(DIRECTION)
(if(LOOK DIRECTION POSITION)
(预计
(SETPOS(看房方向室))
(WHERE))
(progn
...
('CANT'GO'THERE)))
还有一个COND构造:
$ p $ (defun MOVE(DIRECTION)
(cond((LOOK DIRECTION POSITION)
(SETPOS(LOOK DIRECTION ROOM))
(WHERE))
(t
...
(list 'CANT'GO'THERE))))
我也建议从XLISP切换到类似CLISP或ECL。 XLISP很旧,大部分都没有维护,而不是Common Lisp。
Hey guys, simple question...
Working with XLISP to write a program, but I've seemed to run into a simple fundamental problem that I can't seem to work around: perhaps someone has a quick fix.
I'm trying to write an if statement who's then-clause evaluates multiple forms and returns the value of the last.
In example:
(setq POSITION 'DINING-ROOM)
(defun LOOK (DIRECTION ROOM) ... )
(defun SETPOS (ROOM) ... )
(defun WHERE () ... )
(defun MOVE (DIRECTION)
(if (not(equal nil (LOOK DIRECTION POSITION))) ; If there is a room in that direction
( ; Then-block: Go to that room. Return where you are.
(SETPOS (LOOK DIRECTION ROOM))
(WHERE)
)
( ; Else-block: Return error
(list 'CANT 'GO 'THERE)
)
)
The logical equivalent intended is:
function Move (Direction)
{
if(Look(Direction, Room) != null)
{
SetPos(Look(Direction,Room));
return Where();
}
else
{
return "Can't go there";
}
}
(Apologies for the poor web-formatting.)
The problem I have is with:
(
(SETPOS (LOOK DIRECTION ROOM))
(WHERE)
)
I simply want to return the evaluation of WHERE, but I need to execute the SETPOS function first. XLISP doesn't like the extra parentheses: if I remove the outer set, my WHERE list becomes my else (I don't want that). If I remove the sets around SETPOS and WHERE, it treats WHERE like an argument for SETPOS; I also don't want that.
So, how do I simply evaluate the first, then the second and then return the values of the last evaluated?
Lisp usually provides something like PROGN. PROGN evaluates a sequence of expressions and the value(s) of the last expression is returned.
(progn
(do-this)
(do-that))
Also look at your code:
(if (not(equal nil (LOOK DIRECTION POSITION)))
(EQUAL NIL (FOO)) is the same as (NULL FOO)
(NOT (NULL FOO)) is the same as FOO.
So you can simply write:
(if (LOOK DIRECTION POSITION) ... ...)
Or if you want to check if there is a room:
(if (ROOM-P (LOOK DIRECTION POSITION)) ... ...)
ROOM-P would be a predicate that returns T if something is a room.
You may also want to use typical Lisp indentation:
(defun MOVE (DIRECTION)
(if (LOOK DIRECTION POSITION)
(progn
(SETPOS (LOOK DIRECTION ROOM))
(WHERE))
(progn
...
(list 'CANT 'GO 'THERE))))
There is also a COND construct:
(defun MOVE (DIRECTION)
(cond ((LOOK DIRECTION POSITION)
(SETPOS (LOOK DIRECTION ROOM))
(WHERE))
(t
...
(list 'CANT 'GO 'THERE))))
I would also propose to switch from XLISP to something like CLISP or ECL. XLISP is old, mostly not maintained and not Common Lisp.
这篇关于连续调用/评估形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!