21-10-2016, 03:19 PM
1460378443-haskellnew.pdf (Size: 421.36 KB / Downloads: 8)
ABSTRACT
You have probably used computers to do all sorts of useful and
interestingthings. In each application, the computer responds in different ways to your
input, fromthe keyboard, mouse or a file. Still the underlying operations are determined by
thedesign of the program you are given. There are many highlevel languages. One of the
important language among them is Haskell . It is one of the easiest languages to learn and
use, while at the same time being very powerful: It is used by many of the most highly
productive professional programmers.
Haskell is a functional Programing Language. Programming
languages such as C/C++/Java/Python are called imperative programming languages
because they consist of sequences of actions. The programmer quite explicitly tells the
computer how to perform a task, stepbystep. Functional programming languages work
differently. Rather than performing actions in a sequence, they evaluate expressions.
Haskell is extensively used some place s as , Darcs is a revision
control system written in Haskell, with several innovative features, such as more precise
control of the patches to be applied. Cabal is a tool for building and packaging Haskell
libraries and programs. Linspire GNU/Linux chose Haskell for system tools development.
Xmonad is a window manager for the X Window System, written entirely in Haskell.
GHC is also often a testbed for advanced functional programming features and
optimizations in other programming languages. Pandoc is a tool for converting one
markup format into another.
WHY HASKELL PROGRAMING LANGUAGE
Haskell is a general purpose, purely functional programming language
incorporating many recent innovationsin programming language design. Haskell provides
higherorder functions, nonstrict semantics, static polymorphic typing, userdefined
algebraic datatypes, patternmatching, list comprehensions, a module system, and a rich set
of primitive datatypes, including lists, arrays, arbitrary and fixed precision integers, and
floatingpoint numbers. Haskell is both the culmination and solidification of many years of
research on nonstrict functional languages.
Haskell is elegant and concise. Because it uses a lot of high level
concepts, Haskell programs are usually shorter than their imperative equivalents. And
shorter programs are easier to maintain than longer ones and have less bugs. Haskell
programs most often reads out almost exactly like the algorithm description. Which makes it
easier to verify that the function does what the algorithm states.
When you compile your program, the compiler knows which piece
of code is a number, which is a string and so on. That means that a lot of possible errors are
caught at compile time. If you try to add together a number and a string, the compiler will
whine at you. Haskell uses a very good type system that has type inference. That means that
you don't have to explicitly label every piece of code with a type because the type system
can intelligently figure out a lot about it. If you say a = 5 + 4, you don't have to tell Haskell
that a is a number, it can figure that out by itself. Type inference also allows your code to be
more general. If a function you make takes two parameters and adds them together and you
don't explicitly state their type, the function will work on any two parameters that act like
numbers. Haskell provides an elegant, concise and safe way to write your programs.
Programs will not crash unexpectedly, nor produce strangely garbled output.
INTRODUCTION TO HASKELL
Haskell is a computer programming language. In particular, it is a
polymorphically statically typed, lazy, purely functional language, quite different from
most other programming languages. The language is named for Haskell Brooks
Curry, whose work in mathematical logic serves as a foundation for functional
languages. Haskell is based on the lambda calculus; hence the lambda is used as logo.
Haskell offers:
• Substantially increased programmer productivity.
• Shorter, clearer, and more maintainable code.
• Fewer errors, higher reliability.
• A smaller "semantic gap" between the programmer and the language.
• Shorter lead times.
Haskell is a wide spectrum language, suitable for a variety of
applications. It is particularly suitable for programs which need to be highly modifiable and
maintainable. Much of a software product's life is spent in specification, design and
maintenance, and not in programming. Functional languages are superb for writing
specifications which can actually be executed and hence tested and debugged. Such a
specification then is the first prototype of the final program. Functional progrograms are also
relatively easy to maintain, because the code is shorter, clearer, and the rigorous control of
side effects eliminates a huge class of unforeseen interactions.
HISTORY OF HASKELL
Prehistory
A few decades before modern computers were invented, the
mathematician Alonzo Church developed a language called the lambda calculus. He
intended it as a tool for investigating the foundations of mathematics. The first person to
realize the practical connection between programming and the lambda calculus was John
McCarthy, who created Lisp in 1958. During the 1960s, computer scientists began to
recognise and study the importance of the lambda calculus. Peter Landin and Christopher
Strachey developed ideas about the foundations of programming languages. In the early
1970s, Robin Milner created a more rigorous functional programming language named ML.
While ML was developed to help with automated proofs of mathematical theorems, it gained
a following for more general computing tasks. The 1970s saw the emergence of lazy
evaluation as a novel strategy. David Turner developed SASL and KRC, while Rod Burstall
and John Darlington developed NPL and Hope. NPL, KRC and ML influenced the
development of several more languages in the 1980s, including Lazy ML, Clean, and
Miranda.
Early antiquity
By the late 1980s, the efforts of researchers working on lazy
functional languages were scattered across more than a dozen languages. Concerned by this
diffusion of effort, a number of researchers decided to form a committee to design a
common language. After three years of work, the committee published the Haskell 1.0
specification in 1990. It named the language after Haskell Curry, an influential logician.
Many people are rightfully suspicious of “design by committee”, but the work of the Haskell
committee is a beautiful example of the best work a committee can do. They produced an
elegant, considered language design, and succeeded in unifying the fractured efforts of their
research community. Of the thicket of lazy functional languages that existed in 1990, only
Haskell is still actively used. Since its publication in 1990, the Haskell language standard
3
has seen five revisions, most recently in 1998. A number of Haskell implementations have
been written, and several are still actively developed.
During the 1990s, Haskell served two main purposes. On one side,
it gave language researchers a stable language in which to experiment with making lazy
functional programs run efficiently. Other researchers explored how to construct programs
using lazy functional techniques. Still others used it as a teaching language.
The modern era
While these basic explorations of the 1990s proceeded, Haskell
remained firmly an academic affair. The informal slogan of those inside the community was
to “avoid success at all costs”. Few outsiders had heard of the language at all. Indeed,
functional programming as a field was quite obscure. During this time, the mainstream
programming world experimented with relatively small tweaks: from programming in C, to
C++, to Java. Meanwhile, on the fringes, programmers were beginning to tinker with new,
more dynamic languages. Guido van Rossum designed Python; Larry Wall created Perl; and
Yukihiro Matsumoto developed Ruby.
As these newer languages began to seep into wider use, they spread
some crucial ideas. The first was that programmers are not merely capable of working in
expressive languages; in fact, they flourish. The second was in part a byproduct of the rapid
growth in raw computing power of that era: it's often smart to sacrifice some execution
performance in exchange for a big increase in programmer productivity. Finally, several of
these languages borrowed from functional programming. 5 comments
Over the past half a decade, Haskell has successfully escaped from
academia, buoyed in part by the visibility of Python, Ruby, and even Javascript. The
language now has a vibrant and fastgrowing culture of open source and commercial users,
and researchers continue to use it to push the boundaries of performance and expressiveness.
SETING UP HASKELL ENVIRONMENT
Before we start writing our Haskell programs, let's understand how to set up
our Haskell environment. Haskell is available on a wide variety of platforms including
Linux. Try opening a terminal window and type "ghci" to find out if its already installed and
if its already installed a promt of ghci executes in terminal so that you can directly execute
your haskell programs there.
GHC, the most widely used Haskell compiler. GHC can take a Haskell
script (they usually have a .hs extension) and compile it but it also has an interactive mode
which allows you to interactively interact with scripts. The interactive mode is invoked by
typing in ghci at your prompt.
Installing Haskell
To install haskell in Ubuntu its very easy as Haskell Platform is already available
in distribution's package repository.
Step 1: Enter as root using command
Sudo su
then enter your password
Step 2: aptget install haskellplatform
It will install haskell platform in the system
To check if haskell is installed by typing ghci in your terminal , if sucessfully
insatlled the ghci promt executes.
5
Running Haskell:
There are two different ways to start Haskell:
(1) Interactive Interpreter:
You can enter Haskell and start coding right away in the interactive
nterpreter by starting it from the command line. You can do this from Unix, DOS or any
other system, which provides you a commandline interpreter or shell window.
$ghci
Here is the list of all the available command line options:
:add [*]<module> ... add module(s) to the current target set
:load [*]<module> ... load module(s) and their dependents
:break [<mod>] <l> [<col>] set a breakpoint at the specified location
:help Displays a list of the available commands.
(2) Program from the Command line:
A Haskell Program can be executed at command line by invoking
the interpreter on your application, as in the following:
Create file name.hs
Compile using ghc
make name
It will create two file
Name.hs and name.o
U can run using
./name
Note: Be sure the file permission mode allows execution.
FEATURES OF HASKELL
PURE
Haskell is called pure because it does not allow side effects .A side effect is
something that affects the “state” of the world. For instance, a function that prints something
to the screen is said to be side effecting, as is a function which affects the value
of a global variable. Of course, a programming language without side effects would be
horribly useless; Haskell uses a system of monads to isolate all impure computations
from the rest of the program and perform them in the safe way.
Purely functional programs operate only on immutable data. This
is possible because on each modification a new version of a data structure is created
and the old one is preserved. Therefore, data structures are persistent as it is possible to refer
also to old versions of them. If there are no more references to the old version the
unrefered data can be collected by automatic memory management, such as a
garbage collector. Often, bigger data structures share their parts between versions and do
not consume as much memory as all versions separately.
Pure computations yield the same value each time they are
invoked. This property is called referential transparency and makes possible to
conduct equational reasoning on the code. For instance if
y = f x and
g = h y y
then we should be able to replace the definition of g with
g = h (f x)
(f x) will get the same result, only the efficiency might change .
7
STRONGLY TYPED
Haskell has the following build in basic data types:
• Bool : False or True
• Char : the set of Unicode characters
• Integer : arbitrary precision integer numbers
• Int : fixed precision integer, at least [229, 2291]
• Float : real floating point number, single precision
• Double : double precision
We can use the following ways to define complex data types out of other data
types using data constructors :
• Enumeration
• Lists
• Tuples
• Recursion
• List comprehension
Enumeration is similar to the code types of Pascal,i.e. we define directly all
elements of a data type by explicitly mentioning them.
Example:
data Weekday = Mo | Tu | We | Th | Fr | Sa | Su
Mo, Tu, We, Th, Fr, Sa, Su are the data elements of the type Weekday.They are also so
called constructors (they can be used to construct the data type).
Haskell treats lists somewhere between primitive and complex data types,
since it already provides list constructors and does not require to explicitly define lists If a
denotes a data type, then [a] denotes lists over this data type.
[Integer] denotes list containing integers .[] denotes the empty list .
The : constructor can also be used to produce lists.
Tuples are very similar to the record construct in Pascal. Tuples can either use the tuple
8
constructors (,...,) (with n1 commas, if we define an nuple) or, for parameterized tuple
types, we can define our own constructors.
data Date = (Day,Month,Year)
data CartProduct a = Prod a a
a is here a type variable indicating the parameter of the parameterized type.
Haskell allows a programmer to define complex data types using the data statement:
data Mybool = MyFalse | MyTrue
STATICALLY TYPED
Haskell's static type system defines the formal relationship between
types and values .The static type system ensures that Haskell programs are type safe;
that is, that the programmer has not mismatched types in some way. For example, we
cannot generally add together two characters, so he expression 'a'+'b' is ill typed. The main
advantage of statically typed languages is wellknown:
All type errors are detected at compile time. Not all errors are
caught by the type system; an expression such as 1/0 is typable but its evaluation will
result in an error at execution time. Still, the type system finds many program errors at
compile time, aids the user in reasoning about programs, and also permits a compiler to
generate more efficient code (for example, no runtime type tags or tests are required).
The type system also ensures that usersupplied type signatures are correct.
In fact, Haskell's type system is powerful enough to allow us to avoid
writing any type signatures at all; we say that the type system infers the correct types for us.
Nevertheless, judicious placement of type signatures is a good idea, since type
signatures are a very effective form of documentation and help bring
programming errors to light
Haskell automatically infers the most general type. For example, the
9
type of foldr is inferred to be:
(a>b>b)>b>[a]> b,
which can be read as follows: foldr takes as argument a binary operator, whose input values
can be of any types a and b and whose output value is of type b, and returns as its result a
function f’ which takes a value of type b as input and which returns as its result a function
f’’ which takes a list of values of type a as input, and which returns as its result a value of
type b. The inferred type of foldr shows that it can also be used to define functions where the
input types of its first operator argument are different.
FUNCTIONAL
In a functional language, everything (even a program) is a function.
Thus, writing a program is actually writing a function. We can write many functions in a
single file; that is, we can have many programs in a single file. Each function then may use
other functions to calculate its result. There are many predefined functions available like in
other programming languages. These functions are included in the Haskell Prelude and in
the Haskell Libraries. Writing a function consists of two parts.
First, we have to define the types for the arguments and the result. e.g.:
doubleIt :: Integer > Integer
The name of our new function is doubleIt. The :: separates the name of the function from the
type definition. The parameter types are separated from each other and from the return type
by >. Therefore, the declaration above reads “doubleIt is a function with one Integer
parameter, returning Integer.”
The second part of writing a function is the concrete implementation, e.g.:
doubleIt x = 2 * x