2015-05-14 30 views
1

我們想從C#中使用IronScheme調用閉包方案,但是我們不斷得到一個異常,並說「不是一對」。使用ironscheme調用閉包

我們想調用下面的計劃代碼:

(define (create-robot name) 
(let* (
    (position (cons 0 0)) 
    (move-forward(lambda (x) 
       (set! position (cons (car position) (+ x (cdr position)))) 
       position)) 

    ) 
(list name (cons 'position position) (cons 'move-forward move-forward))));return attribute 'name' and procedure 'move-north' 

(define (get-from-robot r name) 
(cdr (assq name (cdr r)))) 

使用C#下面的代碼:

Callable c1 = schemeInterpretor.getCallable("create-robot"); 
Cons john = (Cons)c1.Call("john"); 
Callable getFromRobot = schemeInterpretor.getCallable("get-from-robot"); 
getFromRobot.Call(john , "'position"); 

我們得到以下異常:

{"not a pair"} {&assertion 
&who: "cdr" 
&message: "not a pair" 
&irritants: (#f) 
} 

是什麼導致問題?我們如何解決它?

回答

1

您正在通過string"'position",這是我所見不到的。使用SymbolTable.StringToObject("position")(對象是需要的,因爲如果你將它暴露爲值類型,將會出現裝箱問題)。

您正在收到錯誤,因爲(assq name (cdr r)) => #f正在嘗試將cdr應用到它。