Haskell in Harsh Words


Table of Contents


The Fuck is Haskell?


A pure, lazy, functional programming language. Basically, it is something that happens when like-minded cool people come together.

Meaning of the above bullshit words?

Functional

Pure

Lazy

Haskell, the new cool guy in campus

Types

Abstraction

What can I do with it?

Haskell: A Functional Programming Langauge


What does it mean for Haskell to be a functional programming language? Well, it means that Haskell follows the principle of functional programming — a programming paradigm where functions are the basic building blocks of computation.

Def: A function is a mapping that takes one or more arguments and produces a single result.

Properties of Haskell

First Steps


Haskell comes with a large number of built-in functions, which are defined in a library file called the standard prelude.

Function Format

Just as in lambda calculus haskell follows a fixed pattern for functions. Here are some examples to elaborate:

Examples

-- Program 1
main = print (a ++ b ++ c)
a = [((2**3) * 4)]
b = [(2*3) + (4*5)]
c = [2 + (3 * (4**5))]


-- Program 2
main = print (n)
n = (a `div` (length xs))
    where
        a = 10
        xs = [1 .. 5]


-- Program 3
main = print (a [1 .. 5])
a xs = sum (drop (length xs - 1) (take (length xs) xs))main = putStrLn "hello, world"

Types and class


Types

Classes

Functions


-- Let's see an example
even :: a -> Bool
even n = (n `mod` 2 == 0)

-- Conditional using "if else"
signum n = if n > 0 then 1 
else if n < 0 then -1 
else 0

-- Conditional using "such that"
signum n | n > 0 = 1
	 | n < 0 = -1
	 | otherwise  = 0

Let us now introduce some really cool implementation techniques in haskell with respect to defining functions.

  1. Pattern Matching
    -- Define functions using '_'
    len [] = 0
    len (_:xs) = 1 + len xs
    
    initials :: String -> String -> String  
    initials firstname lastname = [f] ++ ". " ++ [l] ++ "."  where (f:_) = firstname  (l:_) = lastname
    
    -- Defining using '_' fundamentally
    test :: Int -> Int
    test 0 = 1
    test 1 = 2
    test _ = 0
  1. Lambda Functions
    \x -> x + 1
  1. Taking input
    -- Just mentioned here so that one can try out problems with input
    import Control.Arrow ((>>>))
    
    main :: IO ()
    main = interact $
            lines >>> head >>> read >>> solve >>> (++ "\n")
    
    solve :: Int -> String
    
    
    -- Type 2
    import Control.Arrow ((>>>))
    
    main :: IO ()
    main =
      interact $
        words >>> map read >>> solve >>> show >>> (++ "\n")
    
    solve :: [Integer] -> Integer