Why haskell




















Beyond the tight feedback loop during development, Haskell code is easy to refactor and modify. Like real world code written in any other language, such code written in Haskell is not write-only. It will eventually need to be maintained, updated, and extended, often by developers that are not the original authors of the code. With the aid of compile-time checking, many code refactors in Haskell become easy; a common refactoring workflow is to make a desired change in one location and then fix one compiler error at a time until the program compiles again.

This is far easier than the equivalent changes in dynamically typed languages that offer no such assistance to the programmer. Proponents of dynamically typed languages will often argue that automated tests supplant the need for compile-time type checking, and can help prevent errors as well. However, tests are not as powerful as type constraints. For tests to be effective, they must:.

The type system is a fixture in the language and the compiler always validates that the types are correct. The type system is inherently comprehensive, providing full coverage of every piece of Haskell code, and there are no changes to make to it as the underlying code changes. All this is not to say that the type system can replace every type of test.

But what it does do is provide assurances that are more comprehensive than tests, and are present in every code base, even when no tests exist.

GHC, the most commonly used Haskell compiler, produces extremely fast executables, especially when compared against other languages commonly used for application development, such as PHP or Python.

This improved performance leads to both a more responsive application and lower hardware costs. This may be true, but we have found that the difference between Haskell and other languages used for web development is staggering. On one project we worked on in the past, we began implementing new API endpoints in a Haskell web service instead of the incumbent PHP. After around a year of building features and adding endpoints in Haskell, both the PHP and Haskell web services were dealing with a similar average workload in terms of request count and type, and performed similar CRUD actions backed by the same SQL database.

The infrastructure was hosted on AWS, and the breakdown of the infrastructure used for each web service is below. In this application, each of the Haskell and PHP web services handled a similar number of requests, handled a similar workload, and had similar traffic spikes throughout the day, all while querying the same database.

The Haskell endpoints consistently had response times of ms or less, slightly outperforming the PHP endpoints. This difference in cost would scale as the size of the user base, number of MAUs, and underlying infrastructure increased.

The cost reduction that comes with operating Haskell over other web languages is not by any means insignificant. This allows a programmer to create a description of business logic rules that are enforced by the type system.

Haskell has what are referred to as algebraic data types ADTs , consisting of both records product types and tagged unions sum types. Records are similar to dictionaries or JSON objects, and commonly available in many languages. Tagged unions, however, are not available in many languages, but are what enable a significant amount of flexibility in domain modeling.

The power of ADTs is best illustrated through an example. Suppose we are creating an invoicing system that must keep track of customer invoices. Each invoice must contain a list of line items that the invoice is for and have an invoice status that indicates whether the order has been paid or canceled.

The types we would use to model this might look like the following:. Modeling domain rules in the type system like this e.

This is a much stronger set of guarantees than encoding similar rules in class methods, as one might do in an object oriented language that does not have sum types. One application of the above types may be a function that creates a notification message based on the status of the invoice.

This function would take a CustomerInvoice as a parameter and return a string representing the content of the notification. The above function uses pattern matching, another feature in the language, to handle every possible InvoiceStatus value. The case statement allows us to handle the different possible values of the status field. The type system can protect us from making mistakes when changing the rules of our domain. Suppose that after this application is live for a while, we get feedback from our users that we need to be able to refund invoices.

Looks like we forgot to update the createCustomerNotification function to handle this new status value. The compiler is throwing an error and telling us that the case statement does not handle the Refunded value as part of its pattern matches. This protects us from the very common mistake of an unhandled value when writing in dynamically typed languages. The Haskell community has a published a large number of high quality, production grade packages, many of which have been maintained for for a decade or longer.

The Haskell community has general consensus as to which packages are good options in each functional category e. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What is Haskell used for in the real world? Asked 12 years ago. Active 1 year, 8 months ago. Viewed k times. Improve this question. Wojciech Danilo Sergio Tapia Sergio Tapia No, I won't because it's a clear cut answer.

No wiggle room for opinions and such. I just want to know what the language was created for. Papuccino1, wait, do you want to know what it's useful for, or what it was created for? The former is what the question is asking, and there's a lot of room for opinion there Papuccino1 - What benefit do the extra words in your title bring to the question?

Also, I think you're misunderstanding the use of the term "functional" in this context, but it's hard to tell. According to their own website many companies use it for different kinds of purposes Show 4 more comments.

Active Oldest Votes. What are some common uses for this language? Rapid application development. AdvantagesOfFunctionalProgramming : Functional programs tend to be much more terse than their ImperativeLanguage counterparts. Often this leads to enhanced programmer productivity FP encourages quick prototyping.

Apart from this Haskell has its own advantages such as: Clear, intuitive syntax inspired by mathematical notation. List comprehensions to create a list based on existing lists. Lambda expressions: create functions without giving them explicit names. So it's easier to handle big formulas. Haskell is completely referentially transparent. This way, it encourages you to separate code with side effects e.

Lazy evaluation is a really nice feature: Even if something would usually cause an error, it will still work as long as you don't use the result. It is easier to write search programs such as this sudoku solver because it doesn't load every combination at once—it just generates them as it goes along. You can do this in other languages, but only Haskell does this by default. Improve this answer. Callum Watkins 2, 2 2 gold badges 29 29 silver badges 44 44 bronze badges.

Xinus Xinus The following thought often crosses my mind: in a pure language, a function always returns the same result when passed the same parameters. This is a guarantee. Imperative languages in contrast build everything on top of "statements". A statement does not come with any sort of guarantee except its execution consumes time and produces heat. So the foundation is already shaky and everything bult on top of it will remain shaky.

To me this was one of the reasons to learn haskell. It's been 6 years since this question was asked and since the Go programming language debuted. I mean this as a case study; there are clearly things that are holding functional languages back, and they can't all be attributed to hype.

You forgot a point - coding in haskell is an absolute blast ; I've had so much fun trying my hand at some problems in haskell. MartinDrautzburg Of course statements come with guarantees. What sort of useful language would have statements without guarantees? Show 22 more comments. I'm using an iPhone, Mac. Where is Haskell used? Swift can also be functional. It can also be challenging in some languages to convince ourselves that they are correct programs, say when effects are unrestricted or invariants are not protected.

By viewing programming languages as black boxes, we could say that some of those unlock creation of more and more complex high-quality software. And in that respect, programming languages are not so different from deep learning or space travel—they allow us to do something we could not do before. Richard Eisenberg predicted, at ZuriHac , that we might not be using Haskell itself in the distant future.

Perhaps a different language—but it will be built on the valuable discoveries we made with Haskell. For almost every other language, one of the following is true:. Not because those features allow us to solve harder problems the first day they land in GHC the main Haskell compiler. Haskell thrives with ideas. Not only the compiler itself, but also its ecosystem of libraries—Haskellers are in a constant search for better solutions.

Some of these solutions prove to be elegant and powerful, so they stick. Others are soon forgotten. Of course, the same thing happens in other languages, but what is so unique about Haskell is that the language itself acts as a foundation with unique qualities which make its users lean to more principled, although not-so-obvious solutions.



0コメント

  • 1000 / 1000