Sei sulla pagina 1di 7

Ramaiah University of Applied Sciences

Private University Established in Karnataka State by Act No. 15 of 2013

Name: APRATIM RAJ Registration Number: 17ETCS002029

Laboratory 6
Title of the Laboratory Exercise: Introductory exercises in Haskell

1. Introduction and Purpose of Experiment


Students get familiar with the Haskell platform using a set of simple exercises.

2. Aim and Objectives


Aim

● To develop Haskell programs using GHCi


Objectives

At the end of this lab, the student will be able to

● Use GHCi to develop Haskell programs


● Develop Haskell programs edit and execute them successfully

3. Experimental Procedure
Students are given a set of instructions to be executed on the computer. The instructions
should be edited and executed and documented by the student in the lab manual. They are
expected to answer questions posed in section 5 based on their experiment.
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013

Name: APRATIM RAJ Registration Number: 17ETCS002029

Documentation:
a. Procedure and Algorithm(s): Procedure and analysis:
1. How different is this language from ‘C’ programming language?
C programming language is an imperative language, while Haskell
is purely a functional programming language. In imperative
programming, the computer is given a sequence of tasks and
executes them. The state of variables may get changed during the
execution. It basically describes how to solve a problem or task.
However, in functional programming languages like Haskell, what
needs to be accomplished is described, rather than how.
Functional programs are self-contained and stateless. The order of
execution is less important when compared to imperative
programming languages. While the primary flow control involves
loops, conditionals and function calls in languages like C, Haskell
uses function calls and recursion to control program flow.
While designing a program in Haskell, we evaluate functional
transformations and data collections.
There is no notion of variables in Haskell as it is in C.
It is easier to create lists and tuples in Haskell, while their
representative counterparts, arrays and structures take several
lines of code to define and use in C.
C is strictly typed, while Haskell is loosely typed, and C has several
types like byte, short, int and long to represent integer data, but
Haskell has just one.
It is possible to write conditions within expressions in Haskell, but
not in C.
Haskell has built in Boolean data type, but C doesn’t.
Haskell is much more concise, elegant and easier to maintain and
debug. Functional programs are shorter when compared to
imperative equivalents.
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013

Name: APRATIM RAJ Registration Number: 17ETCS002029

2. What are the basic data types here?


A type is a kind of label that every expression has. Even functions
have types. :t gives the type of an expression. Some common
types are:
Int: stands for bounded integers.
Integers: stands for unbounded integers. It can store large
values.
Float: represents floating data with single precision
Double: represents floating point with double precision
Bool: It is Boolean type having one of two values – True or
False
3. How can you create compound data types equivalent to
structures and arrays in Haskell?
We can use lists to represent arrays. A simple list
definition: [1,2..50] creates a list that holds
Numbers from 1 to 50, similar to an array 50 elements,
storing numbers from 1 to 50.
Data structures are nothing but a collection of data of
different type. They can be represented using tuples in Haskell.
Example definition: (09, “Chetan”,20)
4. Do you have variables in Haskell? Explain.
Yes, Haskell has variables, but it uses immutable variables by
default. Immutable means cannot be changed. It appears in
functions/ expressions like DoubleMe x = x*2. Here x is a variable.
Mutable variables can be enabled explicitly.
5. Do you have loops in Haskell? Explain.
Haskell is declarative. It uses recursion as an equivalent to
loops. Loops repeat code while recursion repeats function calls.
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013

Name: APRATIM RAJ Registration Number: 17ETCS002029

b. Conclusions : Haskell is a functional programming language. In simple words, it


tells you what the solution is. It is an entirely different
programming paradigm when compared to imperative
programming. Imperative programs outline how to solve the
problem. Haskell produces concise, robust programs that are
easier to debug and maintain. Writing definitions for arrays, data
structures, loops and functions is easy.
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013

Name: APRATIM RAJ Registration Number: 17ETCS002029

Results and Discussions:

Screenshot:

Discussion:
Here, in this part we are doing some basic math operations, some basic logical operations, using max min to
get the maximum and minimum of two numbers respectively and succ and pred to obtain successor and
predecessor of number respectively.
Here, we load the (*.hs) file and perform the following actions.
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013

Name: APRATIM RAJ Registration Number: 17ETCS002029

Screenshot:

Discussion:
This screenshot represents the Working with (*.hs)file , head, tail, init, null, length, and reverse.
Here in first I got some error while loading .hs file so check the file content that you write the correct code.
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013

Name: APRATIM RAJ Registration Number: 17ETCS002029

Screenshot:

Discussion:
This screenshot represents the working with some more commands like True, lists, take, drop, product etc.

Potrebbero piacerti anche