Question

How can I define a function that returns a closure in maxima?

My concrete example: I have a list of the form:

myList : [a = 1, c = 33, d = c+7];

I want to exctract an element based on the part before the equal sign.

Here's my attempt:

find_a (e) := first(e) = a;
temp1 : sublist(myList ,'find_a );
map('second, temp1);
//output is "1"

This worked as I intended.

Since the real list is much longer and I don't want to copy-paste the code, I now want to use it like this:

SearchElement(ElementName) := block(
        [res],
        res : lambda([x],first(x) = ElementName),
        return (res)
        );
GetByKey(list_,key_) := block(
        [res , finder:SearchElement(key_)],
        res : map('second, sublist(list_,'finder),
        return (res)
        );

GetByKey(myList, a);
// should return "1"

But already the first part doesn't work:

SearchElement(a);
// output:
lambda([x],first(x)=ElementName)
// expected output:
lambda([x],first(x)=a)
Was it helpful?

Solution

This solution worked for me (based upon the "curry" example in the maxima help for buildq, see 36. Function Definition):

ElementSelectorPredicate(targetElement) ::= buildq (
            [targetElement], 
            lambda ([x], first(x) = targetElement)
          );
ExtractElement(targetList, targetElement, operation) := block(
            [searcher: ElementSelectorPredicate(targetElement), res],
            res: first(sublist(targetList,'searcher)), 
            return (operation(res))
          );

Example:

myList : [a = 4, b = 7, c = 5+b]$
ExtractElement(myList, b, identity);
// result: b = 7
ExtractElement(myList, c, second);
// result: b+5
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top