Stan
Good evening all of the class, our exposition is about programming techniques and programming changes.
so, our exposition has differents topics, the first one is about:-
------------------------------------------------------------------------------------------------------------------------------------------
Given a collection of data structures, we define the set T ofall terms by structural induction:
-The basic constructors (constants) are terms:
-If C is a data constructor of arity K, then the objects C X1 ....Xk are terms If X1 are terms of the appropriate type.
The term set is heterogeneous in the sense that appear terms of different types: for example, the structures:
Data [a] = [] | a : [a]
Data arbol a = vacio | nodo (arbola ) a (arbol a)
Datanat = O | sucnat –atención es la letra O mayúscula
Data ent = O’ | S ent | P ent
It’smeaning:
-------------------------------------------------
Nodo vacio [] vacio, [3,4] ,suc(suc O)
Fibonacci numbers
from the definition of fibonacci given computing solution:
fib :: Integer ->Integer
fib 0 = 1
fib 1 = 1
fib (n +2) = fib (n +1) + fib n
This solution has two drawbacks:
* Repeattoo many calculations for computing fib
* Contributions are suspended:
fib 6
-->fib (4+1) + fib 4
-->fib 5 + fib 4
-->fib (3+1) + fib 3 + fib 4
-->fib + fib 3 + fib 4
-->fib (2+1) + fib 2 + fib 3 + fib 4
in this case the memory usage is very inefficient------------------------------------------------------------------------------------------------------------------------------------------
with the example of Fibonacci numbers which give supendidas sums. If the compiler detects estricticidad of arguments, can be forced to do the sums before the turn of the second equation Aux fib:
Aux fib and z (n +1) = (z Aux fib $! (y + z)) n
so, the sum y + z is done before the call:
fibAux 3 4 3
->FibAux 4 7 2
->FibAux 7 11 1
-> Fib Aux 11 18 0
-> 11-------------------------------------------------
and the last sum is not completed, the reader will understand that the previous case, reducing the number may be higher due to the evaluation of the operator ($!), but the number of additions you make is the same. Besides the memory used is much less.
considering the definition of the function that calculates the factorial of a number:
fact :: Int->Int
factn 0 = 1
fact (n +1) = (n +1) * fact n
in this case the memory usage is inefficient because the products are suspended until the evaluation of fact (n-1)
Fact 4
-> (3 +1) * fact 3
-> 4 * fact 3
-> 4 * ((2 +1)) * Fact 2)
-> 4 * (3 * Fact 2)
-> 4 * (3 * ((1 +1) * fact 1))
.......
this efficiency is low, do sums unnecessarily, since the pattern n +1 expressionsappears on the right. a way to improve this is to use a name for the pattern
fact '0 = 1
fact 's' (n +1) = s * Fact' n
-------------------------------------------------
the improvement is noticeable but efficiency is still low.
the factorial you can force that such products are not suspended, but you need an accumulating parameter.
Fact'' 'n = n1 Aux fact
Aux fact n p = if n == 0 then
pelse
Aux fact (n-1) (P * n)
-------------------------------------------------
Products of this form are arranged upside
this requires that the return value has the same type of battery that sub expression.
example
amount [] = 0
sum [x] = x
sum (x: x ': xs) = sum (x + y: xs)
can be extended to other cases as the maximum
more [x] = x
higher (x: x ': xs) = more (max x x': xs)traditional solution
sum = foldl (+) 0
and to force non-stop operations supendidas
sum (x: y: xs) = (λ s -> sum (s: xs) $! (x + y)
folding the izquirda is an accumulator
foldl f z [] = z
foldl f z (x: xs) = foldl f (f z x) xs
to leave no discontinued operations
foldl ':: (a -> b -> a) -> a -> [b] -> a
foldl 'f a [] = a
foldl 'f a (x: xs) = (foldl' f $! f a x) xs
right in the...
Regístrate para leer el documento completo.