Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources for learning idiomatic Haskell (eta reduction, symbolic infix operators, libraries etc.) [closed]

Tags:

idioms

haskell

Despite some experience with Lisp and ML, I'm having a great deal of trouble learning to read and (idiomatically) write Haskell because the local style seems to be

  • do eta elimination whenever possible
  • eschew parentheses in favor of exploiting operator precedence
  • pack half your logic into bucketloads of overloaded, non-alphanumeric infix operators

The last one is particularly difficult because there are so many predefined operators, each with their own conventions and general semantics, that often reading Haskell becomes an exercise in Hoogle and :type.

Are there any good tutorials that assume knowledge of CS/functional concepts, and instead focus on Haskell-specific idioms? I'm looking for something like Real-World Haskell, that starts off with a very naive, explicit program and then gradually transforms it into a more idiomatic style, introducing and explaining the idioms as it goes. But instead of introducing and explaining general concepts like monads and type classes, it would introduce specific monads and specific type classes, like "but this is exactly what the Alternative monoid does!"

like image 632
Wang Avatar asked Nov 18 '11 21:11

Wang


2 Answers

Basic type classes like Show, Eq and Ord should be easy to grasp by reading the library documentation found by Hoogle and/or Haskell-2010 Language Report.

The numeric tower in Haskell seems to be convoluted (Int type is an instance of whooping 11 type classes according to the report), but it is just to support all useful kinds of numbers and number representations mathematicians invented for us: e.g. Integer is a arbitrary size integer, Int is usual machine word-sized integer, and a lazy Peano representation of integers (not in standard library) proved useful in implementation of graph algorithms. The most important numeric type classes are Num and Integral. You can convert between different integer types by using fromIntegral functions. Note also that numerals such as 123 have type Num a => a and there's special type defaulting mechanism designed to reduce the need of type declarations to specify exact numeric type you need. In advanced use cases this works against you so you may want to alter the defaults.

The same situation is with different types of strings: no single representation fits all, so many of them are in the wild: String, Data.ByteString and Data.Text are most important.

Regarding more complicated type classes the best source is Typeclassopedia.

For certain type classes such as Monad, Applicative and Arrow there are a lot of dedicated tutorials and research works. Depending on your math skills, you may also want to read original research papers on the category theory concepts behind the type classes such as excellent "Notions of computation and monads" by Eugenio Moggi.

As for "eta reductions" it is called Point-Free Style. You can get some information from the references mentioned at that link. You can also look at Combinatory Logic, a 1978 paper by John Backus Can programming be liberated from von neumann style? and APL programming language to get a richer historical perspective on the point-free style.

Also there are general books on Haskell such as 'A Gentle Introduction to Haskell' and 'Learn You a Haskell for Great Good'.

As for operator precedence - there are really few operators you must remember: (.), ($) and (>>=) are used much more than everything else (barring arithmetics of course but arithmetic operators are rather unsurprising).

The syntax for tuples and lists seems unproblematic for me too. It is just redundant: foo : bar : [] is the same as [foo, bar] and (,) foo bar is the same as (foo, bar). The prefix versions of tuples of higher arity such as (,,,,) are rarely used.

See also http://www.haskell.org/haskellwiki/Section_of_an_infix_operator for explanation of constructs such as (+ 2) and (2 +) called sections.

Also you can learn from the changes the HLint tool suggests to improve your code. The HLint executable can be installed by cabal install HLint.

As for advanced topics, I can recommend studying purely functional data structures (lets you design efficient immutable data structures and reason about time consumption), call by need lambda calculus (lets you reason about what is evaluated and in which order), System Fc type system (gives you some background on how haskell type checker works), denotational semantic (reasoning about non-termination, partiality and recursion, and also some insight on the notions of strictness, purity and composability).

like image 99
nponeccop Avatar answered Nov 04 '22 12:11

nponeccop


I think the "Write Yourself a Scheme in 48 Hours" tutorial is exactly what you're looking for. This tutorial explains how to implement a Scheme interpreter in Haskell; it's the one document that really made everything click for me.

It has concrete examples of various Haskell idioms and ideas all rolled together in a cool project. This is particularly good for somebody with experience in Lisp, especially if you've read something like SICP or have implemented a Scheme interpreter before. The tutorial is great either way, but it might be a little bit easier to follow if you've done something similar before.

like image 43
Tikhon Jelvis Avatar answered Nov 04 '22 10:11

Tikhon Jelvis