Docsity

Seven Languages in Seven Weeks , Notas de estudo de Engenharia Informática

Pré-visualização parcial do texto

Baixe Seven Languages in Seven Weeks e outras Notas de estudo em PDF para Engenharia Informática, somente na Docsity!

Download from Wow! eBook <www.wowebook.com>

What Readers Are Saying About

Seven Languages in Seven Weeks

Knowing multiple paradigms greatly influences our design abilities, so I’m always on the lookout for good books that’ll help me learn them. This book nicely brings prominent paradigms together. Bruce has experience learning and using multiple languages. Now you can gain from his experience through this book. I highly recommend it. Dr. Venkat Subramaniam Award-winning author and founder, Agile Developer, Inc.

As a programmer, the importance of being exposed to new program- ming languages, paradigms, and techniques cannot be overstated. This book does a marvelous job of introducing seven important and diverse languages in a concise—but nontrivial—manner, revealing their strengths and reasons for being. This book is akin to a dim-sum buffet for any programmer who is interested in exploring new horizons or evaluating emerging languages before committing to studying one in particular. Antonio Cangiano Software engineer and technical evangelist, IBM

Fasten your seat belts, because you are in for a fast-paced journey. This book is packed with programming-language-learning action. Bruce puts it all on the line, and the result is an engaging, reward- ing book that passionate programmers will thoroughly enjoy. If you love learning new languages, if you want to challenge your mind, if you want to take your programming skills to the next level—this book is for you. You will not be disappointed. Frederic Daoud Author, Stripes ...and Java Web Development Is Fun Again and Getting Started with Apache Click

Download from Wow! eBook <www.wowebook.com>

Download from Wow! eBook <www.wowebook.com>

Seven Languages in Seven Weeks

A Pragmatic Guide to Learning

Programming Languages

Bruce A. Tate

The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas

Download from Wow! eBook <www.wowebook.com>

Contents

Report erratum

  • Dedication
  • Acknowledgments
  • Foreword
  • 1 Introduction
    • 1.1 Method to the Madness
    • 1.2 The Languages
    • 1.3 Buy This Book
    • 1.4 Don’t Buy This Book
    • 1.5 A Final Charge
  • 2 Ruby
    • 2.1 Quick History
    • 2.2 Day 1: Finding a Nanny
    • 2.3 Day 2: Floating Down from the Sky
    • 2.4 Day 3: Serious Change
    • 2.5 Wrapping Up Ruby
  • 3 Io
    • 3.1 Introducing Io
    • 3.2 Day 1: Skipping School, Hanging Out
    • 3.3 Day 2: The Sausage King
    • 3.4 Day 3: The Parade and Other Strange Places
    • 3.5 Wrapping Up Io
  • 4 Prolog
    • 4.1 About Prolog
    • 4.2 Day 1: An Excellent Driver
    • 4.3 Day 2: Fifteen Minutes to Wapner
    • 4.4 Day 3: Blowing Up Vegas
    • 4.5 Wrapping Up Prolog
      • CONTENTS
  • 5 Scala
    • 5.1 About Scala
    • 5.2 Day 1: The Castle on the Hill
    • 5.3 Day 2: Clipping Bushes and Other New Tricks
    • 5.4 Day 3: Cutting Through the Fluff
    • 5.5 Wrapping Up Scala
  • 6 Erlang
    • 6.1 Introducing Erlang
    • 6.2 Day 1: Appearing Human
    • 6.3 Day 2: Changing Forms
    • 6.4 Day 3: The Red Pill
    • 6.5 Wrapping Up Erlang
  • 7 Clojure
    • 7.1 Introducing Clojure
    • 7.2 Day 1: Training Luke
    • 7.3 Day 2: Yoda and the Force
    • 7.4 Day 3: An Eye for Evil
    • 7.5 Wrapping Up Clojure
  • 8 Haskell
    • 8.1 Introducing Haskell
    • 8.2 Day 1: Logical
    • 8.3 Day 2: Spock’s Great Strength
    • 8.4 Day 3: The Mind Meld
    • 8.5 Wrapping Up Haskell
  • 9 Wrap-Up
    • 9.1 Programming Models
    • 9.2 Concurrency
    • 9.3 Programming Constructs
    • 9.4 Finding Your Voice
  • A Bibliography
    • Index

D EDICATION 10

After a little while, I learned that Mom was working on another canvas as well. Through her church and through her profession, she’d find broken people. Encountering a lost spouse here or a troubled marriage there, my mother would bring them into class where she would use the paint and paper to slightly open a door that had been slammed shut. As we spent our last week together, person after person would come through her room devastated at the thought of losing their teacher, but Mom would tell the perfect joke or offer the right word of kindness, comforting those who came to comfort her. I got to meet the human canvases who had been put right by the master and gone on to do great things. It was a humbling experience. When I told my mother that I would dedicate this book to her, she said that she would like that, but she had nothing to do with com- puters. That is true enough. The very thought of Windows would leave her helpless. But Mom, you have had everything to do with me. Your well-timed words of encouragement inspired me, your love of creativity shaped me, and your enthusiasm and love of life guide me even now. As I think about these experiences, I can’t help but feel a little better and a little stronger because I, too, am a canvas shaped by the master. This book is dedicated with love to Lynda Lyle Tate, 1936–2010.

Report erratum Download from Wow! eBook <www.wowebook.com> this copy is (P1.0 printing, October 2010)

Acknowledgments

This is the most demanding book I have ever written. It’s also the most rewarding. The people who have offered to help in various ways have made it so. Thanks first and foremost to my family. Kayla and Julia, your writing amazes me. You can’t yet imagine what you can accom- plish. Maggie, you are my joy and inspiration. In the Ruby community, thanks to Dave Thomas for turning me on to the language that turned my career upside down and helped me have fun again. Thanks also to Matz for your friendship and your offer to share your thoughts with my readers. You invited me to Japan to visit the place where Ruby was born, and that experience inspired me much more than you will ever know. To Charles Nutter, Evan Phoenix, and Tim Bray, thanks for the conversations about topics in this book that must have seemed tiresome but helped me refine and shape the message. In the Io community, thanks to Jeremy Tregunna for helping me get plugged in and sharing some cool examples for the book. Your reviews were among the best. They were timely and helped build a much stronger chapter. Steve Dekorte, you’ve created something special, whether or not the marketplace ever recognizes it as so. The concur- rency features rock, and the language has intrinsic beauty. I can def- initely appreciate how much of this language feels right. Thanks for helping this neophyte debug his installation. Thanks also for your thoughtful reviews and your interview that helped me capture the es- sence of Io. You captured the imagination of the beta readers and cre- ated the favorite language of many of them. In the Prolog community, thanks to Brian Tarbox for sharing your remarkable experience with my readers. The dolphin projects, featured on Nova, certainly add a dramatic flair to the Prolog chapter. Special thanks go to Joe Armstrong. You can see how much your feedback shaped the chapter and the overall book. Thanks also for contributing

Download from Wow! eBook <www.wowebook.com>

A CKNOWLEDGMENTS 13

stronger for it. I know that reviewing a book at this level of detail is thankless, demanding work. Those of us who still like technical books thank you. The publishing business could not exist without you. I also want to thank those of you who shared your ideas about language choice and programming philosophy. At various times, Neal Ford, John Heintz, Mike Perham, and Ian Warshak made significant contributions. These kinds of conversations made me look smarter than I really am. Beta readers, thank you for reading the book and keeping me working. Your comments have shown me that a good number of you are working through the languages rather than casually skimming. I’ve changed the book based on hundreds of comments so far and expect to do even more throughout the life of the book. Finally, to the team at the Pragmatic Bookshelf, I offer my sincerest gratitude. Dave Thomas and Andy Hunt, you have had an incalculable impact on my career as a programmer and again as an author. This publishing platform has made writing viable again for me. We can take a book like this one that might not be as attractive to the mass market and make it financially worthwhile. Thanks to all the members of the publishing team. Jackie Carter, your gentle hand and guidance were what this book needed, and I hope you enjoyed our conversations as much as I did. Thanks to those who labored in my shadow to make this book the best it could be. Specifically, I want to thank the team that labored so hard to make this book look good and correct all of my bad habits, including Kim Wimpsett, the copy editor; Seth Maislin, the indexer; Steve Peter, the typesetter; and Janet Furlow, the producer. This book would not be what it is without you. As always, mistakes that slipped through this fine team are all mine. For those of you I missed, I offer my sincerest apologies. Any oversight was not intentional. Finally, thanks to all of my readers. I think that real hard-copy books have value, and I can follow my passion and write because you do, too.

Bruce Tate

Report erratum Download from Wow! eBook <www.wowebook.com> this copy is (P1.0 printing, October 2010)

Foreword

From the yet to be written “How Proust Can Make You a Better Programmer” by Joe Armstrong, Creator of Erlang “The Gmail editor cannot get typographic quotes right.” “Disgraceful,” said Margery, “the sign of an illiterate programmer and a decadent culture.” “What should we do about it?” “We must insist that the next programmer we hire has read all of ‘A la recherche du temps perdu.”’ “All seven volumes?” “All seven volumes.” “Will it make them better at punctuation and make them get their quotes right?” “Not necessarily, but it will make them a better programmer. It’s a Zen thing....” Learning to program is like learning to swim. No amount of theory is a substitute for diving into the pool and flailing around in the water gasping for air. The first time you sink under the water, you panic, but when you bob to the surface and gulp in some air, you feel elated. You think to yourself, “I can swim.” At least that’s how I felt when I learned to swim. It’s the same with programming. The first steps are the most difficult, and you need a good teacher to encourage you to jump into the water. Bruce Tate is such a teacher. This book gives you the opportunity to start with what is the most difficult part of learning to program, namely, getting started.

Download from Wow! eBook <www.wowebook.com>

F OREWORD 16

I’m pleased to see that Bruce’s taste in programming languages is eclec- tic. He covers not only the well-established languages such as Ruby but also less-well-appreciated languages like Io. Ultimately, programming is about understanding, and understanding is about ideas. So, exposure to new ideas is essential to a deeper understanding of what program- ming is all about. A Zen master might tell you that to be better at mathematics you’d better study Latin. Thus it is with programming. To better understand the essence of OO programming, you should study logic or functional programming (FP). To be better at FP, you should study Assembler. Books on comparative programming languages were popular when I grew up as a programmer, but most of these were academic tomes that gave little practical guidance to how to actually go about using a lan- guage. This reflected the technology of the age. You could read about the ideas in a language, but actually trying it out was virtually impos- sible. Today, not only can we read about the ideas, but we can try them in practice. This makes the difference between standing on the poolside wondering whether it would be nice to swim and diving in and enjoying the water. I warmly recommend this book and hope that you enjoy reading it as much as I have.

Joe Armstrong, creator of Erlang 2 March 2010 Stockholm

Report erratum Download from Wow! eBook <www.wowebook.com> this copy is (P1.0 printing, October 2010)

Chapter 1

Introduction

People learn spoken languages for different reasons. You learned your first language to live. It gave you the tools to get through your everyday life. If you learned a second language, the reasons could be very differ- ent. Sometimes, you might have to learn a second language to further your career or adapt to a changing environment. But sometimes you decide to conquer a new language not because you have to but because you want to learn. A second language can help you encounter new worlds. You may even seek enlightenment, knowing every new language can shape the way you think. So it is with programming languages. In this book, I will introduce you to seven different languages. My goal is not to make a motherly demand like your morning spoonful of cod liver oil. I want to guide you through a journey that will enlighten you and change the way you look at pro- gramming. I won’t make you an expert, but I’ll teach you more than “Hello, World.”

1.1 Method to the Madness

Most of the time, when I’m learning a new programming language or framework, I’ll look for a quick interactive tutorial. My goal is to expe- rience the language in a controlled environment. If I want, I can go off script and explore, but I’m basically looking for a quick jolt of caffeine, a snapshot of syntactic sugar, and core concepts. But usually, the experience is not fulfilling. If I want to get the true flavor of a language that is more than a subtle extension of one I already know, a short tutorial is never going to work. I need a deep, fast dive.

Download from Wow! eBook <www.wowebook.com>

T HE L ANGUAGES 19

you a supercharged virtual machine, like Erlang’s BEAM. Because of it, Erlang will let you build fault-tolerant distributed systems much more quickly than you can in other languages. Some lan- guages support programming models that are laser-focused on a particular problem, such as using logic to solve constraints. When you’re through, you will not be an expert in any of these lan- guages, but you will know what each uniquely has to offer. Let’s get to the languages.

1.2 The Languages

Choosing the languages in this book was much easier than you might imagine. I simply asked potential readers. When we rolled up all the data, we had eight potential candidates. I struck JavaScript because it was too popular and replaced it with the next most popular prototype language, Io. I also struck Python because I wanted no more than one object-oriented language, and Ruby was higher on the list. That made room for a surprising candidate, Prolog, which was a top-ten language on the list. These are the languages that did make the cut and the reasons I picked them:

  • Ruby. This object-oriented language gets high marks for ease of use and readability. I briefly considered not including any object- oriented language at all, but I found myself wanting to compare the different programming paradigms to object-oriented program- ming (OOP), so including at least one OOP language was impor- tant. I also wanted to push Ruby a little harder than most pro- grammers do and give readers a flavor for the core decisions that shaped the design of Ruby. I decided to take a dive into Ruby metaprogramming, allowing me to extend the syntax of the lan- guage. I’m quite happy with the result.
  • Io. Along with Prolog, Io is the most controversial language I included. It is not commercially successful, but the concurrency constructs with the simplicity and uniformity of syntax are impor- tant concepts. The minimal syntax is powerful, and the similarities to Lisp are sometimes striking. Io has a small footprint, is a proto- type language like JavaScript, and has a unique message dispatch mechanism that I think you’ll find interesting.
  • Prolog. Yes, I know it’s old, but it is also extremely powerful. Solv- ing a Sudoku in Prolog was an eye-opening experience for me. I’ve worked hard to solve some difficult problems in Java or C Report erratum Download from Wow! eBook <www.wowebook.com> this copy is (P1.0 printing, October 2010)

T HE L ANGUAGES 20

that would have been effortless in Prolog. Joe Armstrong, creator of Erlang, helped me gain a deeper appreciation of this language that strongly influenced Erlang. If you’ve never had an occasion to use it, I think you will be pleasantly surprised.

  • Scala. One of a new generation of languages on the Java vir- tual machine, Scala has brought strong functional concepts to the Java ecosystem. It also embraces OOP. Looking back, I see a strik- ing similarity to C++, which was instrumental to bridging procedu- ral programming and OOP. As you dive into the Scala community, you’ll see why Scala represents pure heresy to pure functional programmers and pure bliss to Java developers.
  • Erlang. One of the oldest languages on this list, Erlang is gather- ing steam as a functional language that gets concurrency, distri- bution, and fault tolerance right. The creators of CouchDB, one of the emerging cloud-based databases, chose Erlang and have never looked back. After spending a little time with this distributed lan- guage, you’ll see why. Erlang makes designing concurrent, dis- tributed, fault-tolerant applications much easier than you could have ever thought possible.
  • Clojure. Another JVM language, this Lisp-dialect makes some rad- ical changes in the way we think about concurrency on the JVM. It is the only language in this book that uses the same strat- egy in versioned databases to manage concurrency. As a Lisp dialect, Clojure packs plenty of punch, supporting perhaps the most flexible programming model in the book. But unlike other Lisp dialects, the parentheses are greatly reduced, and you have a huge ecosystem to lean on, including a huge Java library and widely available deployment platforms.
  • Haskell. This language is the only pure functional language in the book. That means you won’t find mutable state anywhere. The same function with the same input parameters will give you the same output, every time. Of all the strongly typed languages, Haskell supports the most widely respected typing model. Like Prolog, it will take a little while to understand, but the results will be worth it. I’m sorry if your favorite language didn’t make the list. Believe me, I’ve already gotten hate mail from more than a few language enthusiasts. We included several dozen languages in the survey mentioned earlier.

Report erratum Download from Wow! eBook <www.wowebook.com> this copy is (P1.0 printing, October 2010)