• Nenhum resultado encontrado

Now we need to compute the clients miles

N/A
N/A
Protected

Academic year: 2022

Share "Now we need to compute the clients miles"

Copied!
4
0
0

Texto

(1)

Estruturas de Dados / Programação 2 Ponteiros para Funções

Márcio Ribeiro

marcio@ic.ufal.br twitter.com/marciomribeiro

Now we need to compute the clients miles

Different rules to compute miles/points

Suppose that there are similar steps for all airlines…

… and different steps depending on each airline

int compute_miles(int flight_number, int day,

int month, int airline_code) {

// Similar steps for all airlines…

switch (airline_code) { case 0: //american airlines

case 27: //srilankan airlines }

}

Let’s create a func5on for each airline

We can call them within the case statements…

But how can we avoid the switch statement and make our code cleaner and be=er to read and understand?!

int american_airlines(int flight_number) { … } int air_berlin(int flight_number) { … } int british_airways(int flight_number) { … }

int s7_airlines(int flight_number() { … } int srilankan_airlines(int flight_number) { … }

Can we call compute_miles…

… and at the same @me specify for which airline we need to do such a computa@on?!

Instead of passing airlinecode, what about passing the en@re func@on?!

int compute_miles(int flight_number, int day,

int month, airline function) {

// Similar steps for all airlines…

call airline function }

(2)

Pointers to Functions!

Pointer to a func5on

When a func@on is compiled, we have an entry point

When we call the func@on, the entry point is executed

A pointer can contain the address of this entry point

So, we can use the pointer to call the func@on

int american_airlines(int flight_number) { … } int (*airline_function)(int);

airline_function = american_airlines;

Now…

compute_miles has a func@on as parameter and we can easily call this func@on!

int compute_miles(int flight_number, int day,

int month,

int (*airline_function)(int flight_number)) {

// Similar steps for all airlines…

int airline_result = (*airline_function)(flight_number);

}

Another example: areas

Area = ?

double integral(double (*f)(double x), double a, double b) {

double sum, dt;

int i;

sum = 0.0;

dt = (b - a) / 100.0;

for (i = 0; i < 100; i++) sum += (*f)(i * dt + a) * dt;

return sum;

}

double square(double x) {

return x * x;

}

double cube(double x) {

return x * x * x;

}

printf("Integral = %f\n", integral(square, 2, 3));

printf("Integral = %f\n", integral(cube, 2, 3));

(3)

We call this

Higher-order Function!

Higher-order func5on

Takes one or more func@ons as input

Common in func@onal programming (e.g., Haskell)

sum :: Int -> Int -> Int sum x y = x + y

calc :: (Int -> Int -> Int) -> Int -> Int -> Int calc f a b = f a b

Exercises

Exercise 1: map

Takes two inputs (func@on and array) and then applies the func@on to every element of the array. This new array is returned

Implement a func@on map

Call the map func@on with the following func@ons:

Square

Factorial

Now, call map passing an array and the square func@on;

then, do the same for the factorial func@on

Exercise 1: solu5on in Haskell

myMap :: (Int -> Int) -> [Int] -> [Int]

myMap f [] = []

myMap f (a:as) = [f a] ++ myMap f as

Exercise 2: filter

Takes two inputs (func@on and array), where func@on is a test. Filter chucks out any elements of the array that do not sa@sfy the test

Implement a func@on filter

Call the filter func@on with the following func@ons:

even

odd

Now, call filter passing an array and the even func@on;

then, do the same for the odd func@on

(4)

Exercise 2: solu5on in Haskell

myFilter :: (Int -> Bool) -> [Int] -> [Int]

myFilter f [] = []

myFilter f (a:as) = if (f a) then

[a] ++ myFilter f as else

myFilter f as

Why pointers to functions?

Why Haskell?

Recursive functions!

We’re gonna need them in many data structures!

References

Chapter 5 Chapter 1

Referências

Documentos relacionados

(eds.) The Int The Int The Int The Int The International Handbook of En ernational Handbook of En ernational Handbook of En ernational Handbook of Envir ernational Handbook of En

Resultados: Os subtipos mais comuns de TBEB, classificados pelas características na broncoscopia, foram tumoral e granular (em 22,2% para ambas) A baciloscopia de escarro

O luxuoso Contur design da DORMA abriga a avançada tecnologia eletromagnética do operador CS 80 MAGNEO para portas deslizantes, que opera com baixo consumo de energia..

São Camilo Santana Geral PS/INT PS/INT - - PS/INT PS/INT

Composição específica da ictiofauna na Gamboa do Perequê nos meses amostrados, com as freqüências absolutas observadas em número de exemplares (Obs), porcentagens

Potencial = Todos os produtos de mesma CT4 trabalhados pelo ABRADIMEX no canal durante o MATNOV 19;

int mmp_vq MMP_CTX *mmp, gsl_vector *block, int depth, int *code, double *distance.. double acum, min

78 Figura 3.27 Padrão de difração de pós obtido para o complexo ácido nalidíxico:Zn através (a) de síntese por solução, em comparação com os difractogramas