Tracks
/
Elm
Elm
/
Exercises
/
Mario's marvellous lasagna
Mario's marvellous lasagna

Mario's marvellous lasagna

Learning Exercise

Introduction

Let

The let expression is used to create a local scope, and is useful in a few situations:

  • For simplifying expressions
  • For naming the results of intermediate expressions
  • Splitting an expression in to smaller bits if it is getting large

You can do most things inside a let expression, including destructuring (more details on this in later exercises), but it isn't possible to define a new Custom Type or Type Alias.

let
  cube x = x * x * x
in
  cube a + cube b + cube c
beginWithPattern : Size -> Padding -> Pattern -> GameOfLife
beginWithPattern minimumSize padding pattern =
    let
        size = calculateSize minimumSize padding pattern

        center = Size.center size

        centeredPattern = Pattern.centerAt center pattern

        dimensions = { width = size, height = size }

        deadCells = Matrix.create dimensions Dead
    in
    GameOfLife (bringPatternToLife deadCells centeredPattern)

Instructions

You have been given a new cookbook for your birthday, and you want to find out if the lasagna recipe in it is better than your current favourite cookbook. In this exercise you're going to write some code to help you cook the lasagna from the new cookbook.

Your housemate is still busy at the office, and wants to know what time the lasagna will be ready, so that she can make sure she is back in time.

1. Work out what time the lasagna will be ready

Create a function to work out what time the lasagna will be ready. To make the code simple and expressive you should use a let expression.

Define the remainingTimeInMinutes function that takes two parameters and has a let expression as its body. The first parameter is the number of layers in the lasagna, and the second parameter is the number of minutes since you starting cooking.

Define expectedMinutesInOven (in the let part of the let expression) to check how many minutes the lasagna should be in the oven. According to the cookbook, the expected oven time in minutes is 40:

expectedMinutesInOven
    --> 40

Define preparationTimeInMinutes (in the let part of the let expression) that takes the number of layers in the lasagna as a parameter and returns how many minutes it takes to prepare the lasagna, assuming each layer takes 2 minutes to prepare.

preparationTimeInMinutes 3
    --> 6

Return how many minutes are left until the lasagna is finished (in the in part of the let expression), which is the sum of the preparationTimeInMinutes and the expectedMinutesInOven minus the number of minutes since you starting cooking.

remainingTimeInMinutes 3 10
    --> 6 + 40 - 10 = 36
Edit via GitHub The link opens in a new window or tab
Elm Exercism

Ready to start Mario's marvellous lasagna?

Sign up to Exercism to learn and master Elm with 23 concepts, 94 exercises, and real human mentoring, all for free.