## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# Exercises

## Exercise 3-1: Evaluating Expressions

Write a function `sum/1` which, given a positive integer N, will return the sum of all the integers between 1 and N.

Example:

`sum(5) ⇒ 15.`

Write a function `sum/2` which, given two integers N and M, where N =< M, will return the sum of the interval between N and M. If N > M, you want your process to terminate abnormally.

Example:

```sum(1,3) ⇒ 6.
sum(6,6) ⇒ 6.```

## Exercise 3-2: Creating Lists

Write a function that returns a list of the format `[1,2,..,N-1,N]`.

Example:

`create(3) ⇒ [1,2,3].`

Write a function that returns a list of the format `[N`, `N-1,..,2,1]`.

Example:

`reverse_create(3) ⇒ [3,2,1].`

## Exercise 3-3: Side Effects

Write a function that prints out the integers between 1 and N.

Hint: use `io:format("Number:~p~n",[N])`.

Write a function that prints out the even integers between 1 and N.

Hint: use guards.

## Exercise 3-4: Database Handling Using Lists

Write a module `db.erl` that creates a database and is able to store, retrieve, and delete elements in it. The `destroy/1` function will delete the database. Considering that Erlang has garbage collection, you do not need to do anything. Had the `db` module stored everything on file, however, you would delete the file. We are including the `destroy` function to make the interface consistent. You may not use the `lists` library module, and you have to implement all the recursive functions yourself.

Hint: use lists and tuples as your main data structures. When testing your program, remember that Erlang variables are single-assignment: ...

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required