Hi,
 Here is a little thing I came up with to simulate the construct "for
x:= n1 to n2" and "for x:=n1 to n2 by n3"  from purely imperative
world to use in Haskell,  I call the functions fromto  and fromtoby..
they also take a function which consumes the x component and uses it
in the computation.   Just syntactic sugar.. best to wean off of this
way of doing things.. but that is one of the nice things about
Haskell, you CAN do this sort of thing easily.

The definitions:
fromto :: forall b a. Enum a => a -> a -> (a -> b) -> [b]
fromto a b f     = map f [a..b]

-- --------------------------------------

fromtoby :: forall a b.
           (Num a, Enum a) =>
           a -> a -> a -> (a -> b) -> [b]

fromtoby a b c f = map f [a,a+c..b]

-- ------------------------------------------------------
Some applications using ghci with enhancements turned on...

*Iteration> fromto 10 25 id
[10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]  --raw list using id
*Iteration> fromto 10 25 (2*)
[20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50]  -- list times 2

*Iteration> fromtoby 1 12 2 id     -- using id to show what the base list is
[1,3,5,7,9,11]
*Iteration> fromtoby 1 12 2 (flip (^) 3)  -- cubing of the base list above..
[1,27,125,343,729,1331]

*Iteration> fromtoby 12 42 3  id
[12,15,18,21,24,27,30,33,36,39,42]   -- raw list gen'd by  id
*Iteration> fromtoby 12 42 3  (flip (**) 0.3333333333)
[2.2894284849170297, 2.4662120741078493,  -- approx. cube roots
2.6207413939563993,2.7589241761011336,
2.884499140309247,2.999999999670416,
3.1072325056015817,3.2075343296219874,
3.3019272485002094,3.391211442600036,
3.4760266444533747]

Greetings from the Yuma Desert,
gene
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to