2012-03-27 60 views
0

我做的類型數量數據類型,它可以是一個int或真實的,我需要的功能,增加了2個號型的「數字」,而我在使用是什麼問題用定義這樣的語法的語法,我嘗試了以下,但似乎沒有工作。 代碼我寫的有以下幾種,下面的代碼是這樣寫的檢查功能識別電量爲數字類型:標準毫升的數據類型功能

fun plus n:number = "type number"; 

fun plus n:I = "type int of number"; 

fun plus n:number.I = "type int of number"; 

但似乎沒有工作,是我寫作的語法是正確的還是錯誤的? 感謝

的數據類型數量我寫的是:

datatype number = 
    I of int 
| F of real; 

回答

1

你需要模式匹配對你的價值構造IF,並採取一切適當

fun plus (I a) (I b) = I (a+b) 
    | plus (I a) (F b) = ... 
    | etc... 

這給你4案件。當然,你可以也僅2例做,如果你做的是一個解壓縮到number一個real小助手功能,以涵蓋所有,但int - int情況。

+0

我明白了,非常感謝塞巴斯蒂安 – 2012-03-28 04:08:40