O'Reilly logo

Études for Elixir by J. David Eisenberg

Stay ahead with the world's most comprehensive technology and business learning platform.

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

Start Free Trial

No credit card required

Chapter 8. Higher Order Functions and List Comprehensions

Étude 8-1: Simple Higher Order Functions

In calculus, the derivative of a function is “a measure of how a function changes as its input changes” (Wikipedia). For example, if an object is traveling at a constant velocity, that velocity is the same from moment to moment, so the derviative is zero. If an object is falling, its velocity changes a little bit as the object starts falling, and then falls faster and faster as time goes by.

You can calculate the rate of change of a function by calculating: (f(x + delta) - f(x)) / delta, where delta is the interval between measurements. As delta approaches zero, you get closer and closer to the true value of the derivative.

Write a module named Calculus with a function derivative/2. The first argument is the function whose derivative you wish to find, and the second argument is the point at which you are measuring the derivative.

What should you use for a value of delta? I used 1.0e-10, as that is a small number that approaches zero.

Here is some sample output.

iex(1)> c("calculus.ex")
[Calculus]
iex(2)> f1 = fn(x) -> x * x end
#Function<erl_eval.6.17052888>
iex(3)> f1.(7)
49
iex(4)> Calculus.derivative(f1, 3)
6.00000049644222599454
iex(5)> Calculus.derivative(fn(x) -> 3 * x * x + 2 * x + 1 end, 5)
32.00000264769187197089
iex(6)> Calculus.derivative(&:math.sin/1, 0)
1.0
  • Line 3 is a test to see if the f1 function works.
  • Line 5 shows that you don’t have to assign a function to a variable; ...

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

Start Free Trial

No credit card required