This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* returns NONE if list is empty, else SOME (maxnumber) *) | |
fun max(numbers: int list) = | |
if null numbers | |
then NONE | |
else | |
let val max_tail = max(tl numbers) | |
in | |
if max_tail = NONE orelse hd numbers >= valOf max_tail | |
then SOME (hd numbers) | |
else max_tail | |
end; | |
(* concatenates all strings using separator *) | |
fun stringjoin(strings: string list, separator: string) = | |
if null strings | |
then "" | |
else if null (tl strings) | |
then hd strings | |
else hd strings ^ separator ^ stringjoin(tl strings, separator); | |
(* date is tuple of form (year, month, day) -> returns boolean *) | |
fun isLeapYear(date: int*int*int) = | |
#1 date mod 400 = 0 orelse (#1 date mod 4 = 0 andalso #1 date mod 100 <> 0) | |
(* filter all even numbers *) | |
fun filterEven(numbers: int list) = | |
if null numbers | |
then [] | |
else if hd numbers mod 2 = 0 | |
then hd numbers :: filterEven(tl numbers) | |
else filterEven(tl numbers); | |
No comments:
Post a Comment