2017-12-02 93 views
0

我有這樣的功能:共LISP - 減法

(defun test (variable) 
    (cond 
    ((null variable) nil) 
    ((< (- 12 (other-function variable) 3) 0) 1) 
    (t (- 12 (other-function variable) 3)) 
) 
) 

的想法是,如果12的與3的函數的值的減法運算的結果小於0,則返回1,否則,它只會進行減法。 「其他功能」返回一個數字。 當我運行此功能時,lispworks凍結。但是,如果我運行沒有第一個條件的功能:

(defun test (variable) 
    (cond 
    ((null variable) nil) 
    (t (- 12 (other-function variable) 3)) 
) 
) 

它使得減法沒有任何問題。 有人可以幫忙嗎? 謝謝。

編輯: 我試過這樣與設:

(defun test (variable) 
(let (x (other-function variable)) 
    (cond 
    ((null variable) nil) 
    ((< (- 12 x 3) 0) 1) 
    (t (- 12 x 3))) 
) 
) 

,但我仍然得到了同樣的問題lispworks至極,它凍結。當我沒有以下條件運行:

((< (- 12 x 3) 0) 1) 

此功能正常工作。

+0

在我看來,問題在於'other-function'而不是'test'。而不是兩次做同樣的事情,你可以在'let'中綁定這個值並重用這個變量。 – Sylwester

+0

@Sylwester我會添加我試着用這個問題的方式。感謝你的回答。 – MarB

+1

請發表[mcve]。 – melpomene

回答

3

除非您想出完整的代碼和測試用例,否則不能複製。

CL-USER 1 > (lisp-implementation-type) 
"LispWorks" 

CL-USER 2 > (defun test (variable) 
       (cond 
       ((null variable) nil) 
       ((< (- 12 (other-function variable) 3) 0) 1) 
       (t (- 12 (other-function variable) 3)))) 
TEST 

CL-USER 3 > (defun other-function (foo) (+ foo 1)) 
OTHER-FUNCTION 

CL-USER 4 > (test 5) 
3 

CL-USER 5 > (test 500) 
1 

另外:LispWorks通常不會 '凍結' 上的錯誤。