2017-06-18 21 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)