Sayfalar

Haskell etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Haskell etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

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.




23 Mayıs 2014 Cuma

Installation and Compiling Haskell on Ubuntu 13.10

First you should check the universe repository

 sudo gedit /etc/apt/sources.list  and add

deb http://us.archive.ubuntu.com/ubuntu saucy main universe

 line in sources.list if it is not in this file.

Then update : sudo apt-get update

Last install haskell :

sudo apt-get install haskell-platform

Now we try to write first haskell program and build it.


Firstly open a text editor and write

 main = putStrLn "Hello, World!"

Then save as first.hs  In terminal give command like this : ghc -o hello first.hs then ./hello

Welcome the Functional Programming World and Haskell.