2015-05-14 32 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应用到它。