2017-06-18 22 views
1

逻辑:SML语法错误

eploy(列表,常数)

if list is empty then 
    return: 
     0; 
else 
    return: 
     (first_element + constant*eploy(rest_of_the_elements, constant) 

我已经写了下面的代码:

fun eploy(xs, x1:int) = 
     if null xs 
     then (0) 
     else (x::xs') => x + x1*eploy(xs',x1) 

eploy([1,2],4); 

回答

2

如果你想要做的模式,然后匹配您需要使用case

fun eploy(xs, x1) = 
    case xs of 
     nil => 0 
    | x::xs' => x + x1*eploy(xs', x1) 

您也可以合并到这一点的函数定义使用条款:

fun eploy(nil, x1) = 0 
    | eploy(x::xs', x1) = x + x1*eploy(xs', x1)