Skip main navigation

Hurry, only 1 day left to get one year of Unlimited learning for £249.99 £174.99. New subscribers only. T&Cs apply

Find out more

Type Classes

Similar types may be related using type classes. Watch Jeremy Singer introduce some existing type classes in Haskell.

Inevitably, we have glossed over some of the more complex parts of Haskell while we have been introducing the language. Now it’s time to explore one of the ideas we have bumped into on several occasions already — type classes.

What’s the type of 42? Either type 42 into the online interpreter or enter

:t 42

in GHCi. Either way, you should get back Num a => a — whereas you might have thought the answer should be Int. The Num is a type class, which specifies a family of types including integer and floating-point types.

We also anticipated type classes when we used the deriving Show clause to print out our custom data types. Now it’s time to explain type classes in more detail. Please watch the video and think about the concepts. Do type classes remind you of features in other languages? Leave your thoughts in the comments section.

This article is from the free online

Functional Programming in Haskell: Supercharge Your Coding

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now