Learning Haskell Day1
#In Haskell comment line start with -
- . I like it because I use a a lot of times this smile -.- :P an
they are very similar.
#Let define a function and see the
power of the functional programming :)In Haskell everything actually
a function.For example we can define a function like defining a
variable.
Fac 0 = 1
fac n = n* fac(n-1)
- - defining a
funciton
main = print(fac 45)
main program calculate factorial of
45.Of course it is not surprising thing if you familiar with any
programming languages but time is very amazing. Even same
programming languages cannot calculate the factorial of the 40 ,In
Haskell it is very fast also.
Functions can take multiple arguments.
But there must be at least one space between arguments. Passing
arguments is also same. For example
volume x y z = x * y *z
main = print(volume 3 4 5)
this code take tree arguments and
calculate the volume of a rectangular prism.
And finally thing about functions is
where clauses. It is like normal where that in English grammar. But
usage is a little bit different. I think best way is examples for
explanation :).In this code we try to average of the two numbers.
avg a b = s / 2
where s = a+b
main = print(avg 3 4)
if we don't use the where keyword s
will be undefined and compiler give us a error. With where clauses we
defined a variables which used before.
Hiç yorum yok:
Yorum Gönder