You are here:   ArielOrtiz.com > Programming Languages > High-Order Functions

High-Order Functions

Objectives

During this activity, students should be able to:

This activity helps the student develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.

Activity Description

Individually, solve the following set of programming exercises using Erlang. Place all your functions in a module called highorder.

  1. Write the function multiple_apply(ProcList, Val), in which ProcList is a list that contains zero or more one-argument functions, and Val is any value. It returns a list that results from applying every function in ProcList to Val. Examples:
    > P = [fun (X) -> X + X end,
           fun (X) -> X * X end,         
           fun erlang:is_integer/1,       
           fun (X) -> [X] end,           
           fun (X) -> X end].
    [#Fun<erl_eval.6.13229925>,#Fun<erl_eval.6.13229925>,
     #Fun<erlang.is_number.1>,#Fun<erl_eval.6.13229925>,
     #Fun<erl_eval.6.13229925>]
    > highorder:multiple_apply(P, 20). 
    [40,400,true,[20],20]
    > highorder:multiple_apply(P, 1.5).
    [3.0,2.25,false,[1.5],1.5]
    > highorder:multiple_apply([fun erlang:is_number/1, 
                                fun erlang:is_atom/1,
                                fun erlang:is_tuple/1], what).
    [false,true,false]
    
  2. The bisection method is a root-finding algorithm which works by repeatedly dividing an interval in half and then selecting the subinterval in which the root exists.

    Suppose we want to solve the equation f(x) = 0. Given two points a and b such that f(a) and f(b) have opposite signs, f must have at least one root in the interval [a, b] as long as f is continuous on this interval. The bisection method divides the interval in two by computing c = (a+b) / 2. There are now two possibilities: either f(a) and f(c) have opposite signs, or f(c) and f(b) have opposite signs. The bisection algorithm is then applied to the sub-interval where the sign change occurs.

    Write the function bisection(A, B, F) that finds the corresponding root using the bisection method. The algorithm must stop when a value of X is found such that: abs(F(X)) < +1.0e-15.

    Examples:

    > highorder:bisection(1, 4, fun (X) -> (X - 3) * (X + 4) end).
    3.0
    > highorder:bisection(4, 1, fun math:sin/1).
    3.141592653589793
    > highorder:bisection(-1, 1, fun (X) -> math:pow(X, 5) + 5 * X + 1 end).
    -0.19993610217122004
    
  3. The derivate of a function f(x) with respect to variable x is defined as:

    Derivate formula

    Where f must be a continuous function. Write the function derive(F, H) that returns a new function that takes X as input, and which represents the derivate of F given a certain value for H. For example:

    Derivate example
    > F = fun (X) -> X * X * X end.
    #Fun<erl_eval.6.49591080>
    > DF = highorder:derive(F, 0.001).
    #Fun<highorder.0.442056>
    > DF(5).
    75.0150
    > DDF = highorder:derive(DF, 0.001).
    #Fun<highorder.0.442056>
    > DDF(5).
    30.0060
  4. Simpson's rule is a method for numeric integration:

    Simpson's rule formula

    Where h = (b - a) ÷ n, for a given even positive integer n (if you increment the value of n you get a better approximation), and yk = f(a + k × h). Write the function integral(A, B, N, F) that returns the value of the integral, using Simpson's rule.

    For example:

    Integral example

    would be written in Erlang as follows (with n = 10):

    > highorder:integral(0, 1, 10, fun (X) -> X * X * X end).
    0.250000

    The double integral:

    An other integral example

    would be written in Erlang as follows (with n = 10):

    > highorder:integral(
        1, 2, 10,
        fun (X) -> highorder:integral(
                     3, 4, 10,
                     fun (Y) -> X * Y end)
        end).
    5.25000

Deliverables

Using the Online Assignment Delivery System (SETA), deliver the file called highorder.erl. No assignments will be accepted through e-mail or any other means.

IMPORTANT: The program source file must include at the top the author's personal information (name and student id) within comments. For example:

        
%% ITESM CEM, February 19, 2010.
%% Erlang Source File
%% Activity: High-Order Functions
%% Author: Steve Rogers, 449999

    .
    . (The rest of the program goes here)
    .

Due date: Friday, February 19.

Evaluation

This activity will be evaluated using the following criteria:

-10 The program doesn't contain within comments the author's personal information.
10 The program contains syntax errors.
DA The program was plagiarized.
10-100 Depending on the amount of exercises that were solved correctly.
© 1996-2010 by Ariel Ortiz (ariel.ortiz@itesm.mx)
Made with Django | Licensed under Creative Commons | Valid XHTML | Valid CSS