Sayfalar

29 Temmuz 2014 Salı

Haskell Day5

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.  

27 Temmuz 2014 Pazar

Haskell Day4

List and Tuples

Haskell have two important data storage system such as lists and tuples. They are very important thing for Haskell like functions. Lists are implemented by “[ ]” but Tuples with “( )”.The differences between lists and tuples are :
#Tuples size is constant but list are flexible so if we know size of our data we should use tuples for better performance.
#Tuples can be storage different type of data like a Truth value and a string can be in same tuples but list not allow this and return a error which about data types for example a name and a telephone number can be use in a same tuple but they cannot be in a list.

In list and tuples we define elements separating with comas.

list_ex = [1,2,3]
list1_ex = [“ahmet”,”mehmet”,”as”]
tuples_ex = (“john,”,1,True)
main = do
     print(list_ex)
     print(list1_ex)
     print(tuples_ex)


Some mistake coding example

list_ex = [1,”2”,3] – error 1 is a integer but “2” is a char so types are different

we can add elements into a list with using “:”.For example;

list_ex[1,2,3]
main = print(0:list_ex)


program will return 0,1,2,3  

26 Temmuz 2014 Cumartesi

Haskell Day 3

Learning Haskell Day3

Today i discover library of haskell.It is very easy to import a library on our codes. "import" keywords handle everything :)

For example i added System.Info library for using some ready function which implemented before.With this code we can learn system's info about OS or compiler etc.


import System.Info

main=do
print os
print compilerName
print compilerVersion

This code will return for me "linux"  "ghc" and "version versionbranch[7,6]"

Here some basic libraries for Haskell these libraries have some ready functions and data types for using if you interested link look down :)

http://www.haskell.org/ghc/docs/6.12-latest/html/libraries/base-4.2.0.1/

25 Temmuz 2014 Cuma

Haskell Day2


Learning Haskell Day2

         Today I learned about truth values and basic types. Truth values similar to other high-level programming languages. There are two variables such as “True” and “False”. If a condition is not right it will be false otherwise true. For example this program print False

sq x = x * x
main=print(sq 2 ==5 )

Or this one will return true

vol x y z = x*y*z
main=print(vol 3 4 5 == 60).

Of course we can do some boolean operation like “and” and “or”.For and we use && and if one of the condition is false it will be false.For or we use || it is enough to being true if one of them is true.For example:

main=do
      print(3==3 && 4==2)
      print(3==3 || 4==2)
      print((==) 3 5 )

the output like
false true false

first two line is seems normal but third condition is different. The == operator can bu used like a prefix operator even it take two arguments.

Types in Haskell very similar to other programming languages char strings... . If you want to learn a type of a variables on ghci you can use :type ot :t for example :t True will be return boolean.

24 Temmuz 2014 Perşembe

Haskell Day1

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.