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 --- hamming.icl | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 hamming.icl (limited to 'hamming.icl') diff --git a/hamming.icl b/hamming.icl new file mode 100644 index 0000000..e623d7b --- /dev/null +++ b/hamming.icl @@ -0,0 +1,44 @@ +module hamming + +/* +The Hamming Function. + +The result of this program is a list of the first NrElements numbers +having only 2, 3 and 5 as prime factors. Result: + + [1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,...]. + +Run the program with the Show Constructors option on (Application options) +*/ + +import StdEnv + +/* Ham (the Hamming function) returns an infinite list of numbers + having two, three and five as only prime factors by recursively + mapping the functions ((*) 2), ((*) 3) and ((*) 5) over the list of + Hamming numbers found sofar and merging these lists into one. + + The definition of y specifies a cyclic graph to be used yielding a + polynomial complexity in stead of an exponential one. + + The argument lists of merge never end with a Nil + and they are not Nil initially, so + the definition of merge can be specified for 'infinite' lazy lists + leaving out special cases for empty lists. +*/ + +Ham::[Int] +Ham = y +where + y = [1:merge (merge (map ((*) 2) y) (map ((*) 3) y)) (map ((*) 5) y)] + + merge f=:[a:b] g=:[c:d] + | a