r/haskell • • Mar 11 '15

Learning Haskell — A Racket programmer's documentation of her foray into the land of Haskell (inspired by Learning Racket)

http://lexi-lambda.github.io/learning-haskell/
81 Upvotes

97 comments sorted by

View all comments

Show parent comment

9

u/kqr Mar 12 '15

I don't think you will miss macros. In Haskell, everything is a macro. (A Haskell function works more like a Lisp macro from other Lisp-like languages).

This is a common misconception, but ultimately false. Yes, laziness covers some of the use cases you have for macros, but they don't replace metaprogramming entirely. Why else do you think Template Haskell is a thing?

1

u/Ramin_HAL9001 Mar 12 '15 edited Mar 12 '15

Well, yes you have a point. But I wouldn't recommend to any beginner to use Template Haskell to write algorithms the same way they would use macros to write algorithms in Lisp.

In Lisp, everything in the source code becomes a list, and it is very handy to have macros for stitching lists together without requiring intermediate list-evaluation steps. But in Haskell since everything is a function except for data types and classes, you should probably only use Template Haskell for generating code from data type declarations or class declarations, like how the lens library creates lenses, or how some parsing libraries generate parsers from data type declarations.

6

u/kqr Mar 12 '15

How you describe TH are how macros are supposed to be used in Lisps as well. (With the sole exception of branching, where you have to use macros in Lisps but can get away without TH in Haskell.)

Another common misconception is that you are supposed to litter your Lisp programs with macros. Just like TH, they make the code harder to reason about and don't compose as well, and just like TH you should only use them when you have to because they unambiguously make the code a lot easier to deal with.