Reputation: 135
I'm new to Haskell and still haven't figured out how to do anything realistic. I've looked at examples of recurrence relations but the implementation is too advanced for me at the moment.
I'm trying to set up the recurrence relation:
f(t,i) = (2/3)*f(t+1,i+1) + (1/3)*f(t+1,i-1)
but I can't figure out to set my types for the input to f(a,b) or how to define it in general.
Upvotes: 0
Views: 721
Reputation: 1015
Haskell defines functions a little differently from other languages - you don't wrap parameters in brackets, instead they are used in a similar way to maths where it means "do this bit first". So your function would look a little like this in haskell, where you just need a space between your function name and your variables
f t i = (2/3) * f (t+1) (i+1) + (1/3) * f (t+1) (i-1)
Also, to prevent an infinite loop, it's important you create a condition for the recursion to end, for example if you just want to return t when i is zero you can do this (this is a guess, I'm not sure what you'd like your condition to be, you can have more than one)
f t 0 = t
f 10 _ = 10
f t i = (2/3) * f (t+1) (i+1) + (1/3) * f (t+1) (i-1)
It's also considered good practice to add the function type (normally I would do this as my very first step)
f :: Float -> Float -> Float
f t 0 = t
f 10 _ = 10
f t i = (2/3) * f (t+1) (i+1) + (1/3) * f (t+1) (i-1)
Upvotes: 2