[Haskell-beginners] Saving intermediate calculations

Henk-Jan van Tuyl hjgtuyl at chello.nl
Wed Mar 11 00:05:10 UTC 2015


On Mon, 09 Mar 2015 16:47:06 +0100, Abhinav Kalawatia  
<kalawatia_abhinav at yahoo.com> wrote:

:
> week    actual_ sales(a)     forecast(f)                         
> forecast(f)                              forecast(f)
>  1            20                          f1 = a1  
>                              f1 = 20                                     
> f1 = 20
>  2            27                          f2 = f1 + 0.5*(a1 - f1)         
> f2 = 20 +0.5(20-20)                f2 = 20
>  3            25                          f3 = f2 +  0.5*(a2-f2)          
> f3 = 20+0.5*(27-20)               f3=23.5
>  4            22
>
>
> When I execute a function to achieve this in Haskell, I get the forecast  
> for the fourth period. Can I save the values for intermediate  
> period(1..3) also?
>
>
> Please find my Haskell code below:
> a = 0.5
> ipt = [20,27,25,22]
> avg :: [Double] -> Double
> avg (x:xs) = (a*x) + (1-a)*(avg xs)
> avg [] = 0
:

It looks to me, that mapAccumL[0] might do the trick (I haven't studied  
this in detail).

Regards,
Henk-Jan van Tuyl


[0]  
http://haddocks.fpcomplete.com/fp/7.8/20140916-162/base/Data-List.html#v:mapAccumL



-- 
Folding at home
What if you could share your unused computer power to help find a cure? In  
just 5 minutes you can join the world's biggest networked computer and get  
us closer sooner. Watch the video.
http://folding.stanford.edu/


http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--


More information about the Beginners mailing list