Tracks
/
Haskell
Haskell
/
Exercises
/
Lucian's Luscious Lasagna
Lucian's Luscious Lasagna

Lucian's Luscious Lasagna

Learning Exercise

Introduction

Haskell is a functional programming language, there functions are treated as first-class citizens. This means that functions can be passed as arguments to other functions, returned as values from other functions, and assigned to variables.

Functions

Functions in Haskell are defined using the = operator. The name of the function is followed by its arguments, separated by spaces. This is quite different from most other languages where the function name is followed by parentheses containing the arguments separated by commas.

add number1 number2 = number1 + number2

Haskell is a statically typed language, the ghc compiler can infer the types of the arguments and the return value. However, it is a good practice to specify the types of the arguments and the return value. To specify the type of a function, we use the :: operator. The name of the function is followed by the :: operator, then the type of the arguments separated by ->, the last type is the return value.

justNumber :: Int
justNumber = 42

add :: Int -> Int -> Int
add number1 number2 = number1 + number2

Invoking functions

To invoke a function, we write the function name followed by the arguments separated by spaces.

add 1 2

When passing a function as an argument to another function, we can have to use parentheses to specify the order of evaluation.

add (add 1 2) 3

There 1 2 is evaluated first, then the result is passed to the outer add function, so the outer add function recives two arguments.

Indentation / significant whitespace

Haskell uses whitespaces and indentation to specify code boundaries. Code which is part of some expression should be indented further in than the beginning of that expression.

  • Module, import, and top-level function definitions must start at the leftmost column.
  • If an expression is split into multiple lines, the code that is part of that expression must be indented under that expression with at least one space.
  • Parts of the expression that are grouped together should be indented with equal numbers of spaces.
-- A function split over multiple lines, so subsequent lines must be indented
add number1 number2 =
    number1 + number2

https://en.wikibooks.org/wiki/Haskell/Indentation

Modules

Modules export functions. That means you can use the functions the module makes available in its export statement. A module can also define functions only for internal use. By default, functions that are not explicitly exported cannot be used from outside the module.

At the beginning of a module, we specify the module name. If we have a file called Calculator.hs, then we should name our module Calculator. Then, we specify the functions that it exports and after that, we can start writing the functions. So we'll start with this.

module Calculator ( add ) where

https://learnyouahaskell.github.io/modules#making-our-own-modules

Comments

A comment is some text within the Haskell file that is not interpreted as code. It is mainly intended to be read by yourself and other programmers. There is a lightweight syntax for single line comments, based on double dashes. Multiline comments are also possible with the {- and -} pair of opening and closing delimiters.

-- a single line comment
-- another single line comment

{- a multiline comment
   spanning multiple lines
-}

Formatting

There is a style guide, and Stylish-Haskell can be used to automatically format code.

Credits

This concept guide borrows liberally from Learn You a Haskell for Great Good!, under Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

Instructions

In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.

You have five tasks, all related to the time spent cooking the lasagna.

1. Define the expected oven time in minutes

Define the expectedMinutesInOven function that does not take any arguments and returns how many minutes the lasagna should be in the oven. According to the cooking book, the expected oven time in minutes is 40:

expectedMinutesInOven
-- -> 40

2. Calculate the remaining oven time in minutes

Define the remainingMinutesInOven function that takes the actual minutes the lasagna has been in the oven as an argument and returns how many minutes the lasagna still has to remain in the oven, based on the expected oven time in minutes from the previous task.

remainingMinutesInOven 30
-- -> 10

3. Calculate the preparation time in minutes

Define the preparationTimeInMinutes function that takes the number of layers you added to the lasagna as an argument and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.

preparationTimeInMinutes 3
-- -> 6

4. Calculate the elapsed time in minutes

Define the totalTimeInMinutes function that takes two arguments: the first argument is the number of layers you added to the lasagna, and the second argument is the number of minutes the lasagna has been in the oven. The function should return how many minutes in total you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment.

elapsedTimeInMinutes 3 20
-- -> 26

5. Create a notification that the lasagna is ready

Define the alarm function that does not take any arguments and returns a message indicating that the lasagna is ready to eat.

alarm
-- -> "Ding!"
Edit via GitHub The link opens in a new window or tab
Haskell Exercism

Ready to start Lucian's Luscious Lasagna?

Sign up to Exercism to learn and master Haskell with 109 exercises, and real human mentoring, all for free.