diff options
author | Camil Staps | 2016-10-11 12:29:53 +0000 |
---|---|---|
committer | Camil Staps | 2016-10-11 12:29:53 +0000 |
commit | dac20e1e41bbe12b178870d368e7fc56fc12815b (patch) | |
tree | 8250447fc2ff0716c87aaa537bfeb0f5640532c2 /str_arit.icl | |
parent | Initial commit (diff) |
Added simple examples
Diffstat (limited to 'str_arit.icl')
-rw-r--r-- | str_arit.icl | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/str_arit.icl b/str_arit.icl new file mode 100644 index 0000000..a85d9f8 --- /dev/null +++ b/str_arit.icl @@ -0,0 +1,46 @@ +module str_arit + +/* +String Arithmetic. + +This program demonstrates string arithmetic by mergesorting the characters of a large string. +*/ + +import StdEnv + +// *S is needed to create the large string. + +mul_S::Int String -> String +mul_S 0 string = "" +mul_S n string = string +++ mul_S (n-1) string + +// The mergesort algorithm on strings. + +MergeSort::String -> String +MergeSort str + | len<=1 = str + | otherwise = Merge (MergeSort first) (MergeSort second) +where + first = str%(0,middle - 1) + second = str%(middle,len - 1) + middle = len /2 + len = size str + +Merge::String String -> String +Merge str "" = str +Merge "" str = str +Merge str1 str2 + | ch1<ch2 = ch1 +++ Merge (RemoveFirstChar str1) str2 + | otherwise = ch2 +++ Merge str1 (RemoveFirstChar str2) +where + ch1 = str1%(0,0) + ch2 = str2%(0,0) + + +RemoveFirstChar::String -> String +RemoveFirstChar string = string%(1,size string-1) + +// The Start rule: sort a large string (30*40 characters). + +Start::String +Start = MergeSort (mul_S 30 "Sort this garbage properly, please :-). ") |