Professional Documents
Culture Documents
3120 - Merged
3120 - Merged
§ Introduction
§ Characteristics
§ Environment Setup
§ Interactive Modes
§ Basic Arithmetic Manipulation
§ Datatypes
§ Haskell is a general-purpose, statically-typed, purely functional programming
language with type inference and lazy evaluation.
§ Designed for teaching, research and industrial applications, Haskell has pioneered
a number of programming language features such as type classes, which
enable type-safe operator overloading.
§ Haskell's main implementation is the Glasgow Haskell Compiler (GHC). It is named
after logician Haskell Curry.
§ Haskell is a widely used purely functional language.
§ Functional programming is based on mathematical functions.
§ Besides Haskell, some of the other popular languages that follow Functional
Programming paradigm include:
§ Lisp,
§ Python,
§ Erlang,
§ Racket,
§ F#,
§ Clojure, etc.
§ Haskell is intelligent enough to decode your input by looking at the syntax used in
the expression.
§ Take a look at the following example which shows how Haskell treats a List.
§ Lists in Haskell are homogeneous in nature, which means they won’t allow you to
declare a list of different kind of data type.
§ Haskell uses type inference to assign the most logical data type for a given
operation.
§ As a result, we don’t have to declare types if it is “obvious” such as Int vs. Double
values.
§ To explicitly declare the data types, you can add designations after each value like
so
§ 3::Int, 6.4::Float
§ Haskell doesn’t support cross-type operations, meaning have to convert values.
§ Prelude includes type conversions from different common types, such as
fromIntegral or fromDouble.
add 5
Both are separated by space
Output
§ Pattern Matching
§ Guards
§ Where
§ Let
§ Case
§ Pattern matching consists of specifying patterns to which some data should
conform and then checking to see if it does.
§ Define separate function bodies for different patterns.
§ Pattern matching can be performed with data type — numbers, characters, lists,
tuples, etc.
§ Call lucky,
§ The patterns will be
checked from top to
bottom and when it
conforms to a pattern, the
corresponding function
body will be used.
§ The only way a number can
conform to the first pattern
here is if it is 5.
§ If it's not, it falls through to
the second pattern, which
matches anything and
binds it to x.
1. Checking if the number is not in
between 1 and 5
§ Example-
§ In factorial, f(0)=1, f(1)=1
§ factorial :: Int->Int
§ factorial 0 =1
§ factorial n= n * factorial (n-1)
In quicksort, an element that you compare
against is called a pivot. They're
The elements that are smaller than the pivot are light
in green here.
green and elements larger than the pivot are dark green.
§ main = do
§ putStrLn "The addition of the two numbers is:"
§ print(add 2 5) --calling a function
§ Main– main function
§ Do– do will create a chain of different commands and would store it in main
§ printStrLn- to print any string
§ Quit ghci by typing :q
§ Write:
§ ghc –make function.hs
§ Here Haskell compiler will compile the program or (Haskell file .hs) and create number of
files
§ Files-
§ Function.o
§ Function.hi
§ Function.exe
§ Call the script file using the name of it
§ Example- functions.hs
§ The .o is exactly the same as C's object files;
§ the .hi file is an "interface file"; it contains information about the .o that GHC would
need, if you compile other modules, to be able to link against that .o file (said
information cannot be stored in a standard .o file).
§ You could say that the .hi file is the equivalent of C's header files (that is, with .h
extension), only these are generated by GHC from the original Haskell source.
§ Thus, the .hi is used when GHC compiles other modules, and the .o is used when
linking all modules together to produce the executable.
§ You can safely delete the .hi and .o files once you have successfully generated the
executable (or keep them if you want to make some small change and rebuild quickly -
it will save time in unneeded recompilations).
§ To run a file - .\functions
§ If using .\functions.hs
§ It will open a .hs file
§ main = do --do keyword will create a chain of commands for you
§ putStrLn "What is your name?"
§ name <-getLine
§ putStrLn ("hello my name is " ++ name)
§ Every function in Haskell officially only takes one parameter.
§ All the functions that accepted several parameters so far have been curried
functions.
§ This allows functions of several arguments to have some of their initial arguments
partially applied.
§ max (a b)
§ Example- max 4 5
§ Firstly function max takes 4 as an argument and later returns a function that will
process maximum of 4 5
§ https://www.youtube.com/watch?v=psmu_VAuiag
Thompson River University
§ Currying
§ Partial function
§ Every function in Haskell officially only takes one parameter.
§ All the functions that accepted several parameters so far have been curried
functions.
§ This allows functions of several arguments to have some of their initial arguments
partially applied.
§ max (a b)
§ Example- max 4 5
§ Firstly function max takes 4 as an argument and later returns a function that will
process maximum of 4 5
§ div:
§ undefined if the divisor is zero
§ Sometimes called Partial Functions
§ Partial application in Haskell involves passing less than the full number of
arguments to a function that takes multiple arguments.
§ Prelude> add x = x + 3
§ Prelude> p3 = add 3
§
What is p3?
§ Converting a function with multiple arguments into a series of one argument
functions
= g(x)(y)(z)
§ Example-
§ map function
§ filter
§ map takes a function and a list and applies that function to every element in the list,
producing a new list.
filter Filter is a function that takes a predicate and a list and then returns
the list of elements that satisfy the predicate.
list comprehensions
Example-
map (+3) [1,5,3,1,6] is the same as writing [x+3 | x <- [1,5,3,1,6]]
§ That produces a new accumulator value and the binary function is called with that
value and the next element, etc.
§ The right fold, foldr works in a similar way to the left fold, only the accumulator eats
up the values from the right.
§ The foldl1 and foldr1 functions work much like foldl and foldr, only you don't
need to provide them with an explicit starting value. They assume the first (or last)
element of the list to be the starting value and then start the fold with the element
next to it.
§ scanl and scanr are like foldl and foldr, only they report all the intermediate
accumulator states in the form of a list.
the compiler would now complain, because it would think we're trying to apply 4
arguments to the take function,
take 1 filter even [1..10]
§ 4 arguments
§ 1 :: Int,
§ filter :: (a -> Bool) -> [a] -> [a],
§ even :: Integral a => a -> Bool,
§ [1..10] :: [Int].
§ Example-
(take 1) ([2,4,6,8,10])
take 1 [2,4,6,8,10]
[2]
Type Signature of take:
Param1: This param will always return us the same number of elements that we pass here
Param 2: List