2012-10-11 108 views
1

如何定義返回最大值閉包的函數?返回最大值閉包的函數

我具體的例子: 我有以下形式的列表:

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

我想exctract基礎上等號之前的部分元素。

這裏是我的嘗試:

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

這個工作,我打算。

由於真正的名單是更長的時間,我不想複製粘貼代碼, 我現在想用這樣的:

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" 

但已經是第一個部分不工作:

SearchElement(a); 
// output: 
lambda([x],first(x)=ElementName) 
// expected output: 
lambda([x],first(x)=a) 
+0

它是如何與Lisp的? – Ankur

+0

maxima在內部使用lisp – Onur

回答

2

該解決方案爲我(基於在buildq最大值幫助 「咖喱」 的例子,見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)) 
     ); 

例子:

myList : [a = 4, b = 7, c = 5+b]$ 
ExtractElement(myList, b, identity); 
// result: b = 7 
ExtractElement(myList, c, second); 
// result: b+5