About Functions
Today I learned about function
composition thing in Haskell. Firstly I start with simple composition
example. In math we can very easily compose two or more functions
.İ.e : there are two functions f(x) and g(x) and the composition
function can be writed like t(x) = g o f or h(x) = f o g t(x) and
h(x) will different functions. In Haskell we can combine function
with using a functions as a parameter like:
f x = x + 2
g x = x * 3
a = f (g 4)
b =g(f 4)
main = do
print(a)
print(b)
Program will return 14 and 18 .We must
use parentheses otherwise we can use “.”
f ( g x ) = = (f . g) x
Lets start with fundamental thing of
structural programming : “decision part”.In Haskell we use
If,Then and Else structure for making decision. For example there are
three possibility about f(x).
f(x) can be positive or negative or
zero so
mysig x =
if x < 0
– deciding
then -1
– if firs condition ok
what happened
else if x >0
– second condition
then 1
– if second condition ok
what happened
else 0
– what happened if all of
condition is wrong
main=print(mysig (0))
it will return 0.Also this function can
be write like this
mysig x
| x < 0 = -1
| x > 0 = 1
| otherwise = 0
It is so similar to functions in math.