没有合适的资源?快使用搜索试试~ 我知道了~
首页Real World Haskell
Real World Haskell
需积分: 9 29 浏览量
更新于2023-05-23
评论
收藏 8.19MB PDF 举报
函数式语言Haskell超详细教程。Hasell是跨平台的语言,非常接近数学表达。
资源详情
资源评论
资源推荐

29.09.08 14:29Real World Haskell
Seite 1 von 2http://book.realworldhaskell.org/read/
Real World Haskell
by Bryan O'Sullivan, Don Stewart, and John Goerzen
Why functional programming? Why Haskell?
1. Getting started
2. Types and functions
3. Defining types, streamlining functions
4. Functional programming
5. Writing a library: working with JSON data
6. Using typeclasses
7. Input and output
8. Efficient file processing, regular expressions, and file name matching
9. I/O case study: a library for searching the filesystem
10. Code case study: parsing a binary data format
11. Testing and quality assurance
12. Barcode recognition
13. Data structures
14. Monads
15. Programming with monads
16. The Parsec parsing library
17. The foreign function interface
18. Monad transformers
19. Error handling
20. Systems programming
21. Working with databases
22. Web client programming
23. GUI programming
24. Basic concurrent and parallel programming
25. Profiling and tuning for performance
26. Advanced library design: building a Bloom filter

29.09.08 14:29Real World Haskell
Seite 2 von 2http://book.realworldhaskell.org/read/
27. Network programming
28. Software transactional memory
B. Installing GHC and Haskell libraries
C. Characters, strings, and escaping rules
D. Web site and comment system usage and policies
Want to stay up to date? Subscribe to comment feeds for any chapter, or the entire book.
Copyright 2007, 2008 Bryan O'Sullivan, Don Stewart, and John Goerzen. This work is licensed under a
. Icons by Paul Davey
aka Mattahan.
Creative
Commons Attribution-Noncommercial 3.0 License

29.09.08 14:28Why functional programming? Why Haskell?
Seite 1 von 11http://book.realworldhaskell.org/read/why-functional-programming-why-haskell.html
Real World Haskell
Prev Next
Why functional programming? Why Haskell?
Have we got a deal for you!
Haskell is a deep language, and we think that learning it is a hugely rewarding experience. We
will focus on three elements as we explain why. The first is novelty: we invite you to think about
programming from a different and valuable perspective. The second is power: we'll show you
how to create software that is short, fast, and safe. Lastly, we offer you a lot of fun: the
pleasure of applying beautiful programming techniques to solve real problems.
Novelty
Haskell is most likely quite different from any language you've ever used before. Compared to
the usual set of concepts in a programmer's mental toolbox, functional programming offers us a
profoundly different way to think about software.
In Haskell, we de-emphasise code that modifies data. Instead, we focus on functions that take
immutable values as input and produce new values as output. Given the same inputs, these
functions always return the same results. This is a core idea behind functional programming.
Along with not modifying data, our Haskell functions usually don't talk to the external world; we
call these functions pure. We make a strong distinction between pure code and the parts of our
programs that read or write files, communicate over network connections, or make robot arms
move. This makes it easier to organize, reason about, and test our programs.
We abandon some ideas that might seem fundamental, such as having a
for loop built into the
language. We have other, more flexible, ways to perform repetitive tasks.
Even the way in which we evaluate expressions is different in Haskell. We defer every
computation until its result is actually needed: Haskell is a lazy language. Laziness is not merely
a matter of moving work around: it profoundly affects how we write programs.
Power
Throughout this book, we will show you how Haskell's alternatives to the features of traditional
languages are powerful, flexible, and lead to reliable code. Haskell is positively crammed full of
cutting edge ideas about how to create great software.
Since pure code has no dealings with the outside world, and the data it works with is never
modified, the kinds of nasty surprise in which one piece of code invisibly corrupts data used by
another are very rare. Whatever context we use a pure function in, it will behave consistently.
Pure code is easier to test than code that deals with the outside world. When a function only
responds to its visible inputs, we can easily state properties of its behavior that should always
be true. We can automatically test that those properties hold for a huge body of random inputs,
and when our tests pass, we move on. We still use traditional techniques to test code that must
interact with files, networks, or exotic hardware. Since there is much less of this impure code
than we would find in a traditional language, we gain much more assurance that our software is
solid.
by Bryan O'Sullivan, Don Stewart, and John Goerzen
Table of Contents
12 comments
12 comments
10
comments
12 comments
6 comments
10 comments
9 comments
5
comments
5 comments

29.09.08 14:28Why functional programming? Why Haskell?
Seite 2 von 11http://book.realworldhaskell.org/read/why-functional-programming-why-haskell.html
Lazy evaluation has some spooky effects. Let's say we want to find the k least-valued elements
of an unsorted list. In a traditional language, the obvious approach would be to sort the list and
take the first k elements, but this is expensive. For efficiency, we would instead write a special
function that takes these values in one pass, and it would have to perform some moderately
complex book-keeping. In Haskell, the sort-then-take approach actually performs well: laziness
ensures that the list will only be sorted enough to find the k minimal elements.
Better yet, our Haskell code that operates so efficiently is tiny, and uses standard library
functions.
It can take a while to develop an intuitive feel for when lazy evaluation is important, but when
we exploit it, the resulting code is often clean, brief, and efficient.
As the above example shows, an important aspect of Haskell's power lies in the compactness of
the code we write. Compared to working in popular traditional languages, when we develop in
Haskell we often write much less code, in substantially less time, and with fewer bugs.
Enjoyment
We believe that it is easy to pick up the basics of Haskell programming, and that you will be able
to successfully write small programs within a matter of hours or days.
Since effective programming in Haskell differs greatly from other languages, you should expect
that mastering both the language itself and functional programming techniques will require
plenty of thought and practice.
Harking back to our own days of getting started with Haskell, the good news is that the fun
begins early: it's simply an entertaining challenge to dig into a new language, in which so many
commonplace ideas are different or missing, and to figure out how to write simple programs.
For us, the initial pleasure lasted as our experience grew and our understanding deepened. In
other languages, it's difficult to see any connection between science and the nuts-and-bolts of
programming. In Haskell, we have imported some ideas from abstract mathematics and put
them to work. Even better, we find that not only are these ideas easy to pick up, they have a
practical payoff in helping us to write more compact, reusable code.
Furthermore, we won't be putting any “brick walls” in your way: there are no especially difficult
or gruesome techniques in this book that you must master in order to be able to program
effectively.
That being said, Haskell is a rigorous language: it will make you perform more of your thinking
up front. It can take a little while to adjust to debugging much of your code before you ever run
it, in response to the compiler telling you that something about your program does not make
sense. Even with years of experience, we remain astonished and pleased by how often our
Haskell programs simply work on the first try, once we fix those compilation errors.
What to expect from this book
We started this project because a growing number of people are using Haskell to solve everyday
problems. Because Haskell has its roots in academia, few of the Haskell books that currently
exist focus on the problems and techniques of everyday programming that we're interested in.
With this book, we want to show you how to use functional programming and Haskell to solve
realistic problems. This is a hands-on book: every chapter contains dozens of code samples, and
many contain complete applications. Here are a few examples of the libraries, techniques and
tools that we'll show you how to develop.
-- file: ch00/KMinima.hs
-- lines beginning with "--" are comments.
minima k xs = take k (sort xs)
22 comments
No comments
7 comments
3 comments
11
comments
3 comments
4 comments
1
comment
7 comments
1 comment
6 comments
3
comments
2 comments

29.09.08 14:28Why functional programming? Why Haskell?
Seite 3 von 11http://book.realworldhaskell.org/read/why-functional-programming-why-haskell.html
tools that we'll show you how to develop.
Create an application that downloads podcast episodes from the Internet, and stores its
history in an SQL database.
Test your code in an intuitive and powerful way. Describe properties that ought to be true,
then let the QuickCheck library generate test cases automatically.
Take a grainy phone camera snapshot of a barcode, and turn it into an identifier that you
can use to query a library or bookseller's web site.
Write code that thrives on the web. Exchange data with servers and clients written in other
languages using JSON notation. Develop a concurrent link checker.
A little bit about you
What will you need to know before reading this book? We expect that you already know how to
program, but if you've never used a functional language, that's fine.
No matter what your level of experience is, we have tried to anticipate your needs: we go out of
our way to explain new and potentially tricky ideas in depth, usually with examples and images
to drive our points home.
As a new Haskell programmer, you'll inevitably start out writing quite a bit of code by hand for
which you could have used a library function or programming technique, had you just known of
its existence. We've packed this book with information to help you to come up to speed as
quickly as possible.
Of course, there will always be a few bumps along the road. If you start out anticipating an
occasional surprise or difficulty along with the fun stuff, you will have the best experience. Any
rough patches you might hit won't last long.
As you become a more seasoned Haskell programmer, the way that you write code will change.
Indeed, over the course of this book, the way that we present code will evolve, as we move from
the basics of the language to increasingly powerful and productive features and techniques.
What to expect from Haskell
Haskell is a general purpose programming language. It was designed without any application
niche in mind. Although it takes a strong stand on how programs should be written, it does not
favour one problem domain over others.
While at its core, the language encourages a pure, lazy style of functional programming, this is
the default, not the only option. Haskell also supports the more traditional models of procedural
code and strict evaluation. Additionally, although the focus of the language is squarely on writing
statically typed programs, it is possible (though rarely seen) to write Haskell code in a
dynamically typed manner.
Compared to traditional static languages
Languages that use simple static type systems have been the mainstay of the programming
world for decades. Haskell is statically typed, but its notion of what types are for, and what we
can do with them, is much more flexible and powerful than traditional languages. Types make a
major contribution to the brevity, clarity, and efficiency of Haskell programs.
Although powerful, Haskell's type system is often also unobtrusive. If we omit explicit type
information, a Haskell compiler will automatically infer the type of an expression or function.
Compared to traditional static languages, to which we must spoon-feed large amounts of type
information, the combination of power and inference in Haskell's type system significantly
reduces the clutter and redundancy of our code.
Several of Haskell's other features combine to further increase the amount of work we can fit
into a screenful of text. This brings improvements in development time and agility: we can
2 comments
No comments
3 comments
No comments
No comments
11 comments
1 comment
2 comments
1 comment
No
comments
No comments
5 comments
7 comments
3 comments
剩余600页未读,继续阅读


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0