Reader small image

You're reading from  Soar with Haskell

Product typeBook
Published inDec 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781805128458
Edition1st Edition
Languages
Right arrow
Author (1)
Tom Schrijvers
Tom Schrijvers
author image
Tom Schrijvers

Tom Schrijvers is a professor of computer science at KU Leuven in Belgium since 2014, and previously from 2011 until 2014 at Ghent University in Belgium. He has over 20 years of research experience in programming languages and has co-authored more than 100 scientific papers. Much of his research focuses on functional programming and on the Haskell programming language in particular: he has made many contributions to the language, its ecosystem and applications, and chaired academic events like the Haskell Symposium. At the same time, he has more than a decade of teaching experience (including functional programming with Haskell) and received several teaching awards.
Read more about Tom Schrijvers

Right arrow

Functions

While FP has been around for many decades, its following has only been growing rapidly in recent years. More programmers are picking up functional programming languages or are programming in a functional style in non-functional languages than ever before. At the same time, existing non-functional programming languages are adopting a growing number of FP features and libraries.

In these ongoing developments, Haskell stands out as a reference for all. As a purely FP language, it embodies the ideal of FP. As a trailblazer for new (functional) programming language developments, it sets the bar for other languages to follow in its tracks.

Before diving into practical FP, this chapter gives a brief overview of what FP is and how Haskell fits in. Then, we start with our first Haskell functions. We learn how to define and call functions. This includes an explanation of all the syntactic elements (types, type signature, function body, and so on) and their role. Along the way...

Technical requirements

From the Writing basic functions section onward, you will need a working Haskell environment: the Glasgow Haskell Compiler (GHC) and a code editor (e.g., Visual Studio). We refer to the Haskell website (in particular, the page at https://www.haskell.org/get-started/) for directions on how to set up this environment. The main installer for GHC can be found at https://www.haskell.org/ghcup/.

The code shown in this book can be downloaded from its GitHub repository: https://github.com/PacktPublishing/Soar-with-Haskell.

What is FP?

Before diving into practical Haskell programming, we give a brief overview of FP, its history, and its applications. If you are eager to get your hands dirty, you may want to forge ahead to the next section, then return here at a later occasion when you are ready to put Haskell into context.

Programming with functions

Functional programming (FP) is one of the main programming paradigms next to imperative programming and object-oriented programming.

Declarative programming

What sets FP apart from the other two is that it is a member of the declarative programming family. Sometimes, the principle of declarative programming is summarized by saying that declarative programs state what should happen, not how it should happen. This means that its programs do not explicitly determine the order in which computation steps are executed; it is up to the language implementation. Haskell particularly stands out among other FP languages because it embraces the declarative...

Writing basic functions

In this section, we write our first Haskell functions to get acquainted with Haskell’s syntax and basic elements.

Our first function

Let us start with a simple function for incrementing an integer:

increment :: Int -> Int
increment x = x + 1

This function definition consists of two lines. The first line is the type signature and the second line defines the behavior of the function. The type signature states that the function has the name increment and, given a value of the Int type as input, produces a result of the Int type. Here, Int is of course the type of integers such as -1, 0, and 42.

The second line is an equation that says that increment x (where x is any possible input) is equal to x + 1. We can read such an equation also operationally: given any x input, the increment function returns the result x + 1. Here, x is called a variable; it acts as a placeholder for an actual input to the function. The result x + 1 is called the...

Programming with primitive types

Haskell comes with several built-in primitive types that are used in most programs.

Int and Integer

We have already used the Int type of integers in several examples. It supports four common arithmetic infix operators:

  • (+)addition
  • (-)subtraction
  • (*)multiplication
  • (^)exponentiation

The (-) operator can also be used as a prefix operator to negate a number. Besides these operators, two useful arithmetic functions are as follows:

  1. divinteger division
  2. modmodulo

A common beginner mistake is to use the (/) operator for Int, but it is only defined for floating-point types such as Float and Double.

The Int type only covers a finite range of integers. The Haskell language specification guarantees that this covers at least the integers in the range from -229 to (229-1), but the actual range can be implementation dependent. For example, in GHC 8.10.2...

Putting the type checker to work

Types are very important in Haskell. It is, after all, a statically typed language. This means that programs are (type-)checked before they are run, by a process called the type checker. If the type checker finds that the program violates the typing discipline imposed by the language, then it raises a (type) error and the program will not be executed.

The type checker helps in several ways during the programming process.

Checking function calls

Firstly, when calling a function, it checks that we pass parameters of the appropriate type to that function. For instance, recall that discount has the Int -> Float type:

*Main> discount True
<interactive>:44:10: error:
    • Couldn't match expected type 'Int' with actual type 'Bool'
    • In the first argument of 'discount', namely 'True'
      In the expression...

Combining functions

You can write larger Haskell programs by composing simple functions into more complex ones.

Calling functions from within functions

Functions are composed simply by defining a more complex function in terms of simpler functions. This means that the definition of the complex function calls other functions.

For example, let us write a function to compute the price of a purchase given the price of the purchased item and the quantity at which it is purchased:

price :: Float -> Int -> Float
price ip qty = ip * fromIntegral qty

This is already an example of the principle that a more complex function, price, calls simpler functions. In this case, the simpler functions are two predefined functions: the (*) operator and the fromIntegral function. Recall that the fromIntegral conversion is needed to convert the Int quantity to a Float type before it can be multiplied by the item price.

When our business logic evolves, we can introduce a discounted price...

Summary

This chapter has given us a brief introduction to FP and Haskell. We have written our first basic Haskell functions, used Haskell’s primitive types, and combined individual functions into larger programs. We have also learned about type checking and how it helps us.

In Chapter 2, Algebraic Datatypes, we will learn how to define our own custom datatypes, how to create values of these types with constructors, and how to take those values apart with pattern matching. We will also familiarize ourselves with a powerful abstraction mechanism for types that is used ubiquitously in Haskell: parametric polymorphism.

Questions

  1. What are the key characteristics of the Haskell language?
  2. What is the purpose of parentheses in Haskell?
  3. What is the difference between the Int and Integer types?
  4. How does the type checker help in the programming process?
  5. What are the different ways in which we can define local functions?

Further reading

  • Haskell 98 Language and Libraries: The Revised Report. Simon Peyton Jones et al. December 2002. https://www.haskell.org/onlinereport/
  • A history of Haskell: being lazy with class. Paul Hudak, John Hughes, Simon L. Peyton Jones, Philip Wadler. HOPL 2007: 1-55.

Answers

  1. Haskell is a lazy, purely FP language that is both principled and nimble.
  2. Parentheses are used for overriding or disambiguating the precedence of function and operator applications.
  3. Because the Int type uses a fixed-size representation, it has a bounded range and wraps around when going beyond that range. In contrast, because the size of an Integer value is not fixed, it can be arbitrarily large.
  4. The type checker makes sure that function definitions conform to their type signature and that function calls likewise respect that type signature. It also disambiguates overloaded functions and operators, and can automatically infer type signatures when none are given.
  5. Haskell provides two different syntaxes for this. With letin…, we can define a local function anywhere in an expression. In contrast, a where clause can define a function local to an equation.
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Soar with Haskell
Published in: Dec 2023Publisher: PacktISBN-13: 9781805128458
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Tom Schrijvers

Tom Schrijvers is a professor of computer science at KU Leuven in Belgium since 2014, and previously from 2011 until 2014 at Ghent University in Belgium. He has over 20 years of research experience in programming languages and has co-authored more than 100 scientific papers. Much of his research focuses on functional programming and on the Haskell programming language in particular: he has made many contributions to the language, its ecosystem and applications, and chaired academic events like the Haskell Symposium. At the same time, he has more than a decade of teaching experience (including functional programming with Haskell) and received several teaching awards.
Read more about Tom Schrijvers