Haskell Programming
Haskell Programming
Lazy Evaluation: Haskell uses lazy evaluation, also known as call-by-need evaluation. This
means that expressions are not evaluated until their results are actually needed. Lazy
evaluation can lead to more efficient programs, especially when dealing with infinite data
structures.
-* Static Typing and Type Inference: Haskell has a strong, static type system. However,
Haskell's type inference allows most types to be inferred by the compiler, reducing the need
for explicit type declarations. This helps catch type-related errors at compile-time, making
programs more robust.
Immutable Data: Data in Haskell is immutable, meaning once a data structure is created, it
cannot be changed. Any operation that appears to modify a data structure actually creates a
new copy with the desired changes. Immutability simplifies reasoning about the behavior of
This means that expressions are not evaluated until their results are actually needed. Lazy
evaluation can lead to more efficient programs, especially when dealing with infinite data
structures.This means that expressions are not evaluated until their results are actually
needed. Lazy evaluation can lead to more efficient programs, especially when dealing with
infinite data structures.
functions to take other functions as arguments or return them as results. This feature enables
powerful abstractions and expressive code, facilitating the use of functions as building blocks
for complex behaviors.
Type Classes and Polymorphism: Haskell's type classes allow for ad-hoc polymorphism.
Type classes define a set of functions that can work on any type that is an instance of that
class. That enables code reuse and generic programming while ensuring type safety.
Concurrency and Parallelism: Haskell provides robust support for concurrent and parallel
programming. Haskell's pure functional nature makes it well-suited for concurrent
programming, and its runtime system supports lightweight threads and multicore parallelism.
Strong Community and Package Ecosystem: Haskell has a vibrant and active community
that contributes to libraries and tools, creating a rich ecosystem. The Haskell Platform
provides a comprehensive collection of libraries for various purposes, enabling developers to
leverage existing solutions for their projects.
-- Subtraction
-- Floating-point number type difference :: Int
y :: Float difference = 10 - 4 -- results in 6
y = 3.14
-- Multiplication
-- Double-precision floating-point productResult :: Int
z :: Double productResult = 3 * 7 -- results in 21
z = 3.141592653589793
-- Division
quotient :: Float
quotient = 10 / 3 -- results in 3.3333...
-- List of Integers
numbers :: [Int]
numbers = [1, 2, 3, 4, 5]
combinedList :: [Int]
combinedList = [1, 2, 3] ++ [4, 5, 6] -- results in [1, 2, 3, 4, 5, 6]
-- Accessing Elements by Index (0-based)
elementAtIndex :: Int
elementAtIndex = numbers !! 2 -- gets the third element (index 2), which is 3
firstElement :: Int
firstElement = head numbers -- gets the first element, which is 1
restOfList :: [Int]
restOfList = tail numbers -- gets the list without the first element, [2, 3, 4, 5]
-- List Comprehension
evenNumbers :: [Int]
evenNumbers = [x | x <- numbers, x `mod` 2 == 0] -- results in [2, 4]
-- Pair of Integers
pair :: (Int, Int)
pair = (1, 2)
-- Accessing Elements
name :: String
name = fst person -- gets the first element of the tuple, "Alice"
age :: Int
age = snd person -- gets the second element of the tuple, 30
Inbuilt Data Types
Nothing: Represents the absence of a value, indicating that the computation has
failed or that no valid result is available.
-- Output:
-- "The number is 42 and isTrue is True"
Example 2:
result :: String
result = funcomp 5
-- The result will be "35" because (5^2) + 10 = 35, and then it's converted to a string
Functions
For first-time users, guards can look very similar to If-Else statements,
but they are functionally different.
Recursion:
Higher Order Function
Till now, what we have seen is that Haskell functions take one type as
input and produce another type as output.
Higher Order Functions are a unique feature of Haskell where you can use
a function as an input or output argument.
-- Usage examples
doubleTwice :: Int -> Int
doubleTwice = applyTwice double
-- Usage
result :: Int
result = square 5
-- Result: 25 (5 * 5 = 25)
let x = [1..10]
print (x)
print (head x)
print (tail x)
print (last x)
print (init x)
print (null x)
print (reverse x)
print (length x)
main = print(take 5 ([1 .. 10]))
main = print(drop 5 ([1 .. 10]))
print (maximum x)
print (minimum x)
print (sum x)
print (product x)