【发布时间】:2017-06-09 14:20:42
【问题描述】:
我写了一个函数,它计算一个由实数列表组成的多项式的 x 值。
infixr 5 ^^;
fun (x:real) ^^ 0 = 1.0
| (x:real) ^^ n = x*(x^^(n-1));
fun poly [] (x:real) = 0.0
| poly (hd::tl) (x:real) = hd*(x^^(length tl)) + poly tl x;
这段代码运行良好,我为此感到非常自豪。
我已经设法使用部分应用创建多项式函数:
> fun f x = poly [3.0,2.0,1.0] x;
val f = fn : real -> real
> f 2.0;
val it = 17.0 : real
创建数学函数:f(x) = 3*x^2 + 2*x + 1
这一切都很好,但我希望能够通过这种方法构造一个函数:
fun f x = polyGen [1.0,2.0,3.0];
它会给我一个与上述功能等效的功能。 这可能吗? 我知道这似乎微不足道,我可以像以前一样在其中放一个 x,然后继续我的生活。但我只是好奇有人会如何解决这个问题!
提前致谢,夏兰
编辑:
fun polyGen L = let fun poly [] x = 0.0
| poly (hd::tl) x = hd + x*(poly tl x);
in fn x => poly L x end;
可爱!
【问题讨论】:
标签: functional-programming sml currying partial-application