still trying to make closures work

2 views
Skip to first unread message

Sean McIlroy

unread,
Aug 1, 2008, 5:55:59 PM8/1/08
to KeyKit
HEY TIM

CLOSURES ARE APPARENTLY A STUBBORN PROBLEM. I'VE TRIED A COUPLE THINGS
(THE FUNCTION-BASED APPROACH YOU SUGGESTED AND ALSO AN OBJECT-BASED
APPROACH) WITHOUT SUCCESS. PROBABLY I HAVEN'T UNDERSTOOD WHAT YOU WERE
GETTING AT (SEE BELOW FOR MY TAKE ON IT). COULD YOU TAKE A LOOK AT THE
CODE BELOW AND SEE IF I'M DOING SOMETHING OBVIOUSLY WRONG? I'M NOT
ATTACHED TO EITHER APPROACH; I'M JUST HOPING TO GET THIS TO WORK
SOMEHOW.

#) THIS STUFF WORKS:

function list(...) {return(argv(0,nargs()))}

function tail(list)
{
returnvalue = []
for (i=1; i<sizeof(list); i++) returnvalue[i-1]=list[i]
return(returnvalue)
}

function union(list1,list2)
{
accumulator = list1
offset = sizeof(list1)
for (index in list2) {accumulator[offset+index] = list2[index]}
return(accumulator)
}

function testpolynomial(a,b,c,d) {return(2*pow(a,3)+3*pow(b,2)+5*pow(c,
2)+4*pow(d,3))}

#) THIS STUFF DOESN'T WORK:

#) HERE IS MY UNDERSTANDING OF THE SOLUTION YOU SUGGESTED:
function evalclosure(closure,variablebindings) {return(closure[0]
(varg(union(tail(closure),variablebindings))))}

#) HERE IS MY ATTEMPT AT AN OBJECT-BASED SOLUTION:
class closure
{
method init(function,constantbindings) {$.function=function;
$.constantbindings=constantbindings}
method evaluated(variablebindings)
{return($.function(varg(union($.constantbindings,variablebindings))))}
}

functionsuccess = testpolynomial(1,2,3,4)==evalclosure(list(polynomial,
1,2),list(3,4))
objectsuccess =
testpolynomial(1,2,3,4)==closure(testpolynomial,list(1,2)).evaluated(list(3,4))

AS MENTIONED, functionsuccess==objectsuccess==0. BUMMER !!

peace
stm

Tim Thompson

unread,
Aug 1, 2008, 6:44:41 PM8/1/08
to Sean McIlroy, KeyKit
> I'VE TRIED A COUPLE THINGS
> (THE FUNCTION-BASED APPROACH YOU SUGGESTED AND ALSO AN OBJECT-BASED
> APPROACH) WITHOUT SUCCESS.

A function-based approach (i.e. something that is closer to the example I
gave) will probably be easier to debug, if you're having trouble. Less
things to go wrong.

I don't have time right now to look at it in detail, but:

1) I notice you're using "function" as variable and element names - I'm
surprised that doesn't throw an error right away - you should use
non-reserved keywords for element names.

2) Break up complex statements into several statements, and add print
statements liberally to see what's going on.

...Tim...

Message has been deleted

Sean McIlroy

unread,
Aug 2, 2008, 3:17:03 PM8/2/08
to KeyKit
> To instanciate an object you must use the 'new' keyword:

AHA !! that's got it. thanks a bunch.

stm


Reply all
Reply to author
Forward
0 new messages