terça-feira, 31 de janeiro de 2012

Haskell Types

Types exists to classify identities (according to their characteristics).
In Haskell every expression have a associated type
e :: T
means that the type of expression e is T.

58 :: Int
'a' :: Char
[1, 2, 3] :: [Int]
('a', 3) :: (Char, Int)

  • Basic Types
Bool Boolean True, False
Char Characters 'a', 'B', '1', '\n', ...
Int Integers with limited size 1, -8, 234345, ...
Integer Integers with unlimited size 1, -8, 54634785464345345, ...
Float Numbers with float coma 3.5, 4.0, -6.4356, 51.2e7, 3e4, ...
Double Numbers with float with double precision 3.5, -6.342342345543, 51.2e7, ...

quinta-feira, 26 de janeiro de 2012

Basic Functions

A basic function in Haskell can be compared to a math function.
  • Incrementer
f(x)=x+1
What this function do is to transform a number x into a number x+1 (ex: f(1)=2).

In  Haskell, we write this function by the same way, where f is the name of the function and x is the argument and x+1 is the result.

The type of this f function would be
f :: Int -> Int
Note that the f function receives an int value (x) and returns another int (x+1)
(Click here to get more info about types)

So this function can be written like this:
f :: Int->Int
f x = x+1
  • Greater
Now, imagine a function called greater that receives two numbers and check if the first is greater than the second, e.x. greater 4 3 = True; greater 3 4 = False.
The type of this function is
greater :: Int -> Int -> Bool
And should be,initially, declared by this way

greater x y = if(x>y) then True else False

But the function >, declared at Prelude (where the basic functions, such as the sum, greater, etc, are declared), already returns a boolean value (True or False), so the declaration of the greater function can be done by this way:

greater x y = x>y

or in the extreme case

greater = >

Hello Haskell

Hello, my name is Paulo and I'm in Informatics Engineering at Universidade do Minho.
Here you have some basic tips to use the functional programming languague called Haskell.

  • A Brief History of Haskell
Following the release of Miranda by Research Software Ltd, in 1985, interest in lazy functional languages grew: by 1987, more than a dozennon-strict, purely functional programming languages existed. Of these, Miranda was the most widely used, but was not in the public domain. At the conference on Functional Programming Languages and Computer Architecture (FPCA '87) in Portland, Oregon, a meeting was held during which participants formed a strong consensus that a committee should be formed to define an open standard for such languages. The committee's purpose was to consolidate the existing functional languages into a common one that would serve as a basis for future research in functional-language design.
in Wikipedia 
  • Getting Started
  1. Install Haskell - http://hackage.haskell.org/platform/
  2. Install Haskell Editor - this step is optional, since you can use any text editor (such as notepad) to write a Haskell program, but other editors, such as Notepad++ can help you in keeping your code clean and organized. (Note: the file extension should be .hs)