Started Learning Haskell

Started Learning Haskell

Why Haskell?

For a long I wanted to learn something that will challenge my way of thinking about solving a problem. There is a huge horror around "What is a Monad?" meme. I want to know about it(also wants to read Category Theory for Programming.) monad meme.jpg

And Haskell is Purely Functional. functional.jpg

I installed Haskell compiler / interpreter(ghc/ghci)

So, as it is a custom in computer science world that everything will start with Hello World! . We can do it in Haskell pretty simple as follows.

main :: IO ()
main = print "Hello World!"

Before diving more into code, let us try to understand the features of Haskell.

Features

  • Statically Typed

    All Haskell values have a type. Every expression in Haskell has a type which is determined at compile time.
    char = 'a' :: Char
    int = 123 :: Int
    
  • Type Inference

    As the type should be known at compile, you can specify explicitly in the program or You can leave it to compiler to decide for you.

  • Lazy Evaluation

    Lazy evaluation is the main objective behind developing the Haskell language. It means an expression will not be evaluated until their results were needed in computation.

  • Purely Functional

    Every function in Haskell is a function in the mathematical sense (...pure). eg. mathematical function f(x,y) = x+y => f(2,3) = 5 function in Haskell

    add x y = x + y
    add 2 3
    

    gives the output of 5.
    There are no statements or instructions, only expressions which cannot mutate variables (local or global) nor access state like time or random numbers.

  • Concurrent

    Haskell lends itself well to concurrent programming due to its explicit handling of effects.

few code snippets I tried today.

Square

square x = x*x

Comments

-- This is a single line comments
{-
This
is 
a 
multiline 
comment
-}

Factorial

fact n = product [1..n]
fact 10
-- gives factorial of 10.

[1..10] is a list 1 to 10 == [1,2,3,4,5,6,7,8,9,10]

Map

map (+1) [1..5]
-- [2,3,4,5,6] adds 1 to every item in the list

Filter

filter (>5) [3..7]
--[6,7] filter values greater than 5.

I think it will be enough for today. See you again. There is an interactive section to try Haskell at Haskell Website if you have 5 min.