DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Day 7 - Haskell

Day 7 - Haskell

Wayne Adams user avatar by
Wayne Adams
·
Apr. 06, 11 · Interview
Like (0)
Save
Tweet
Share
4.58K Views

Join the DZone community and get the full member experience.

Join For Free

I've reached the 7th and final language of Bruce Tate's Seven Languages in Seven Weeks. While some of the previous languages were functional with some imperative support, Haskell is a purely functional, and statically typed, language. I'm looking forward to seeing how I will perform tasks with no (or little -- more on that below) state.

Bruce starts with the usual investigation of types, including looking at Haskell's type inference, then moves on to functions. Functions are composed of a type declaration and the function declaration. Because Haskell is statically typed, a function must have a type, but the declaration isn't strictly necessary because of Haskell's type inferencing. He covers some examples using Erlang-familiar pattern matching and guards. As with many of the other languages in this book, Haskell uses tail-recursion optimization. Next we look at programming with tuples, and using tuples to combine functions. We follow with a discussion of Haskell lists, including generating them with recursion, ranges, and list comprehension (a feature shared with Erlang). By the way, Bruce's explanation of list comprehension here is one of the many reasons I like this book. I didn't quite catch it the first time, but his elaboration here is quite articulate.

Day 1 of Haskell finishes with an interview with Philip Wadler, a member of the committee which designed Haskell. One thing I took away from this interview was the variety of organizations in which Haskell is being used, which is very encouraging. I've been doing OO for 20 years now and I suppose I'm just looking around for evidence that it can be safe and effective to give up mutable state. I may actually decide to learn Haskell first, then pick up more Scala, as a way of disciplining myself to stick with the paradigm.

Day 2 starts with higher-order functions, including anonymous functions and partially-applied functions. Bruce then discusses some common functions, like filter and the Haskell versions of fold-left and fold-right.

Bruce spends a little more time discussing function composition. Every function in Haskell has only one parameter, which, as you can guess, leads to some interesting functional composition requirements to perform any useful computation. You can define a function with multiple parameters, but Haskell will split the function into multiple functions, one with each argument, a procedure known as partial application, or currying. Next he discusses lazy evaluation and how, as in Clojure, it can be used to create infinite lists, where only the parts necessary to perform a task are actually calculated. He finishes this section by describing how problem solutions can be constructed by chaining/nesting functions.

Haskell was created by committee. One side effect of this process is that a number of talented individuals not only contributed to the language, but are potentially available to share their thoughts on the genesis of the language. Bruce next interviews Simon Peyton-Jones, another member of the Haskell creation committee, and the lead designer of the GHC compiler. Peyton-Jones makes one observation that really sticks with me -- Haskell is a useful, successful language, designed by committee! How often does that happen?

Day 3 begins with a deeper dive into Haskell's type system, which Bruce calls one of its strongest features. As I mentioned earlier, the typing is static, but Haskell is smart enough to infer types most of the time, sparing some boilerplate. Developers can define their own data types with the data keyword. He then discusses defining polymorphic data types and recursive data types (the latter of which could be used to define a tree, for example).

Next is a discussion of Haskell classes, with a side note that these are not object-oriented classes. There is no data involved in a Haskell class! So a class, as Bruce puts it, "defines which operations can work on which inputs." So a type is an instance of a class if it supports all the functions signatures which make up the definition of the class.

Monads are discussed next. Bruce describes some example problems which are awkward to formulate in a functional language as a motivation for the use of monads, which are basically a technique of function composition. When I referred earlier to absence of state, but with a qualification, I was looking ahead to monads. Monads allow you to simulate state in a Haskell program. He goes on to outline the components of a monad, which are a container-based type constructor, a return function which wraps a function into a monad, and a bind function that unwraps the function. He then gives a few non-trivial monad-construction examples, including the use of the Haskell do notation to add some syntactic sugar. Bruce ends his Day 3 of Haskell with a discussion of the language's core strengths and weaknesses.

My Personal Wrap-Up

I started reading this book with some understanding and hands-on experience with Scala, and a slight feeling of lost opportunity as a result (Was this a good idea? Will I really learn functional programming with a "compromise" language?). I still feel that way a little, but I still expect to move forward with Scala as my bridge to the FP world, although with a better understanding of what compromises I am making in the process. I'm considering learning Haskell, at least minimally, to see what it's like for me to work with a language that doesn't have the Java/JVM escape hatch in it, so that I don't automatically reach for that pill just because it's available. Hopefully that experience will encourage me to use Scala's functional side first, before reaching for the Java escape hatch.

Concerning this book: Bruce Tate is a very articulate, clear and engaging writer. I see a couple of books a year that speak very directly to me. Maybe I think these books are superior simply because they happen to have been written in a style that is especially suited to the way I think and learn. With that acknowledgment of likely bias, I have to say Seven Languages in Seven Weeks is definitely one of those "once or twice a year" books for me. I really recommend you pick up a copy if you have a desire for a shallow-dive into a variety of languages.

From http://wayne-adams.blogspot.com/2011/04/day-7-haskell.html

 

Haskell (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Container Security: Don't Let Your Guard Down
  • Spring Boot, Quarkus, or Micronaut?
  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Tracking Software Architecture Decisions

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: