Calling closure using ironscheme

115 Views Asked by At

We would like to call a scheme closure from C# using IronScheme, but we keep getting an exception, saying "not a pair".

We would like to call the following Scheme code:

(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))))

Using the following code in C#:

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

We get the following exception:

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

What is causing the problem? How can we solve it?

1

There are 1 best solutions below

0
On BEST ANSWER

You are passing in a string with "'position", which is not expected from what I can see. Use SymbolTable.StringToObject("position") (object is needed as there will be boxing issues if you expose it is as a valuetype).

You are getting the error because (assq name (cdr r)) => #f and you are trying to apply cdr to it.