以表格形式连续调用/评估?
嘿伙计们,简单的问题...
使用 XLISP 编写程序,但我似乎遇到了一个我似乎无法解决的简单基本问题:也许有人有一个快速解决方案。
我正在尝试编写一个 if 语句,该语句的 then 子句评估多种形式并返回最后一种形式的值。
在示例中:
(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)
)
)
逻辑等效项是:(
function Move (Direction)
{
if(Look(Direction, Room) != null)
{
SetPos(Look(Direction,Room));
return Where();
}
else
{
return "Can't go there";
}
}
对糟糕的网络格式表示歉意。)
我遇到的问题是:
(
(SETPOS (LOOK DIRECTION ROOM))
(WHERE)
)
我只想返回 WHERE 的评估,但我需要先执行 SETPOS 函数。 XLISP 不喜欢额外的括号:如果我删除外部集合,我的 WHERE 列表就会变成我的 else(我不希望这样)。如果我删除 SETPOS 和 WHERE 周围的集合,它会将 WHERE 视为 SETPOS 的参数;我也不想这样。
那么,我如何简单地评估第一个,然后评估第二个,然后返回最后评估的值?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Lisp 通常提供类似 PROGN 的东西。 PROGN 计算一系列表达式并返回最后一个表达式的值。
另外看看你的代码:
所以你可以简单地写:
或者如果你想检查是否有房间:
ROOM-P 将是一个谓词,如果某物是房间,则返回 T。
您可能还想使用典型的 Lisp 缩进:
还有一个 COND 构造:
我还建议从 XLISP 切换到 CLISP 或 ECL 之类的东西。 XLISP 很旧,大部分都没有维护,也不是 Common Lisp。
Lisp usually provides something like PROGN. PROGN evaluates a sequence of expressions and the value(s) of the last expression is returned.
Also look at your code:
So you can simply write:
Or if you want to check if there is a room:
ROOM-P would be a predicate that returns T if something is a room.
You may also want to use typical Lisp indentation:
There is also a COND construct:
I would also propose to switch from XLISP to something like CLISP or ECL. XLISP is old, mostly not maintained and not Common Lisp.
所以我找到了一种连续执行的方法(无论是否是最好的方法):
Changed:
To:
执行两种形式并返回最后一种形式的输出。
So I found a way of doing consecutive executions (whether its the best means or not):
Changed:
To:
Which executed both forms and returned the output of the last.