Online Book Reader

Home Category

Learn You a Haskell for Great Good! - Miran Lipovaca [0]

By Root 418 0
Learn You a Haskell for Great Good!


Miran Lipovaca


Copyright © 2011

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.

No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.

No Starch Press

* * *

Introduction

Haskell is fun, and that’s what it’s all about!

This book is aimed at people who have experience programming in imperative languages—such as C++, Java, and Python—and now want to try out Haskell. But even if you don’t have any significant programming experience, I’ll bet a smart person like you will be able to follow along and learn Haskell.

My first reaction to Haskell was that the language was just too weird. But after getting over that initial hurdle, it was smooth sailing. Even if Haskell seems strange to you at first, don’t give up. Learning Haskell is almost like learning to program for the first time all over again. It’s fun, and it forces you to think differently.

Note

If you ever get really stuck, the IRC channel #haskell on the freenode network is a great place to ask questions. The people there tend to be nice, patient, and understanding. They’re a great resource for Haskell newbies.

So, What's Haskell?


Haskell is a purely functional programming language.

In imperative programming languages, you give the computer a sequence of tasks, which it then executes. While executing them, the computer can change state. For instance, you can set the variable a to 5 and then do some stuff that might change the value of a. There are also flow-control structures for executing instructions several times, such as for and while loops.

Purely functional programming is different. You don’t tell the computer what to do—you tell it what stuff is. For instance, you can tell the computer that the factorial of a number is the product of every integer from 1 to that number or that the sum of a list of numbers is the first number plus the sum of the remaining numbers. You can express both of these operations as functions.

In functional programming, you can’t set a variable to one value and then set it to something else later on. If you say a is 5, you can’t just change your mind and say it’s something else. After all, you said it was 5. (What are you, some kind of liar?)

In purely functional languages, a function has no side effects. The only thing a function can do is calculate something and return the result. At first, this seems limiting, but it actually has some very nice consequences. If a function is called twice with the same parameters, it’s guaranteed to return the same result both times. This property is called referential transparency. It lets the programmer easily deduce (and even prove) that a function is correct. You can then build more complex functions by gluing these simple functions together.

Haskell is lazy. This means that unless specifically told otherwise, Haskell won’t execute functions until it needs to show you a result. This is made possible by referential transparency. If you know that the result of a function depends only on the parameters that function is given, it doesn’t matter when you actually calculate

Return Main Page Next Page

®Online Book Reader