/** This file is part of Shapes. ** ** Shapes is free software: you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation, either version 3 of the License, or ** any later version. ** ** Shapes is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with Shapes. If not, see . ** ** Copyright 2008, 2013, 2014, 2015 Henrik Tidefelt **/ ##needs ..Shapes..Data / seq-support ##lookin ..Shapes ##lookin ..Shapes..Data lst: [Data..range begin:'4 end:'10] /** We can measure the length of a sequence by folding a function that adds one for each element. ** Since the entire sequence needs to be consumed, and the order of visits does not matter, ** both left and right folds may be used, but a left fold will be more efficient. **/ IO..•stdout << `Length: ´ << [lst.foldl (\ p e → '1+p) '0] << "{n} /** This is to show the order in which the state is modified when folding with state from left and right. **/ [lst.foldsl \ p e •dst → { •dst << e p } void IO..•stdout ] [lst.foldsr \ e p •dst → { •dst << e p } void IO..•stdout ] IO..•stdout << "{n} /** A classic example -- the sequence of Fibonacci numbers. **/ Fib: [cons '0 [cons '1 [map2 (+) Fib Fib.cdr]]] /** We square each number in the sequence. **/ Fib2: [map (\ x → x*x) Fib] IO..•stdout << `A short sequence of Fibonacci numbers:´ << "{n} IO..•stdout << [seq_string [take '7 Fib] >> [separate ` ´ ...]] << "{n} IO..•stdout << `... and the same sequence squared:´ << "{n} IO..•stdout << [seq_string [take '7 Fib2] >> [separate ` ´ ...]] << "{n}