Define function in Haskell with computations in arguments -
how can define function in haskell this:
x(0) = 1 x(2*k) = sin(k) * cos(k) x(2*k+1) = cos(k/2) - tan(2*k)
haskell not allow define function computation in arguments. raise "parse error in pattern" error.
if try define in terms of x(k)
, this:
x(k) = sin(k/2) * cos(k/2) x(k) = cos((k/2-1)/2) - tan(k/2-1)
i have 2 definitions x(k)
, not valid.
you can define follows:
f 0 = 1 f | = sin(k) * cos(k) | otherwise = cos(k/2) - tan(2*k) k = fromintegral $ div 2
i replaced x
f
because x
looks more variable function. furthermore used i
incoming parameter.
div 2
means divide i
2
(and floor result), such 0
maps 0
, 1
0
, 2
1
. 1 can use fromintegral
convert floating
; otherwise cannot take sin
, etc.
now there 2 cases (except f 0
case): 1 i
even
(2*k
) , 1 i
odd
(in case otherwise
). can use guards this. case, use sin(k) * cos(k)
, other 1 cos(k/2)-tan(2*k)
. note k/2
use floating point, if i
7
, k
3
, calculate cos(1.5)-tan(6)
.
Comments
Post a Comment