From dac20e1e41bbe12b178870d368e7fc56fc12815b Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 11 Oct 2016 12:29:53 +0000 Subject: Added simple examples --- str_arit.icl | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 str_arit.icl (limited to 'str_arit.icl') 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 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 :-). ") -- cgit v1.2.3