ϵ">]>
2. CVIČENIE Z PREDMETU ÚVOD DO DEKLARATÍVNEHO PROGRAMOVANIA
http://ii.fmph.uniba.sk/cl/courses/1-AIN-505-udp/0708ls/ex/ex02.cl
Dátum: streda 20. a utorok 26. 2. 2006
Odporúčaná verzia CL: 5.81.19
WWW stránka predmetu: http://ii.fmph.uniba.sk/cl/courses/1-AIN-505-udp/0708ls/?lang=sk
Kontakt na vyučujúcich: voda_zavináč_fmph.uniba.sk, kluka_zavináč_fmph.uniba.sk
Assignment 1. Your task is to
define the arithmetic operations below as dicussed in the lecture on recursive definitions;
use the above defined application displays to display these functions;
apply the defined function to several different arguments in queries.
Within your definitions, you can only use the successor function S() = , the discrimination
where is a new variable and those built-in operations that you have already defined as functions. For example, you may use the built-in addition operator + to define the multiplication function .
Úloha 1. Vašou úlohou je
zadefinovať doleuvedené aritmetické operácie spôsobom rozoberaným na prednáške;
použiť horeuvedené zobrazenia aplikácií na zobrazovanie týchto funkcií;
aplikovať zadefinované funkcie na niekoľko rôznych argumentov v queries.
V definíciách môžete použiť iba funkciu nasledovník S() = , diskrimináciu
v ktorej je nová premenná, a tie zabudované operácie, ktoré ste už zadefinovanovali ako funkcie. Napríklad na definíciu funkcie násobenia môžete použiť zabudovaný operátor sčítania +.
(e) Division/Delenie
Hint: In this definition, you may use the discrimination:
Návod: V tejto definícii môžete použiť diskrimináciu:
(e*) How can you replace the discrimination | using only the functions defined above and the discrimination | ? Define a function accordingly.
Ako môžete diskrimináciu | nahradiť použitím iba vyššie definovaných funkcií a diskriminácie | ? Definujte takto funkciu .
(g) Greatest common divisor/Najväčší spoločný deliteľ
Hint: Use the euclidean algorithm. Two versions of this algorithm are known: one with subtraction, another with the remainder operation. Use the remainder version.
Návod: Použite euklidovský algoritmus. Tento algoritmus existuje v dvoch verziách: s odčítaním a so zvyškom. Použite verziu so zvyškom.
Assignment 2. Define the following functions by recursion (even if there is a simpler way). You an use built-in arithmetic operation. However, do not use subtraction, it is not necessary in any of these definitions.
Úloha 2. Ďalej definujte rekurziou nasledujúce funkcie (aj keď je aj jednoduchšia možnosť). Môžete používať zabudované aritmetické operácie. Nepoužívajte však odčítanie, na žiadnu z týchto definícií nie je potrebné.
(d) Fibonacci function/Fibonacciho funkcia
The value is the -the element (counting from 0) of the Fibonacci sequence whose first two elements are 1, and each subsequent element is the sum of its two preceding elements.
Hint: You have to distinguish the cases , , and for some . Recall that a .
Hodnotou je -tý prvok (počítajúc od 0) Fibonacciho postupnosti , ktorej prvé dva prvky sú 1 a každý ďalší prvok je súčtom predchádzajúcich dvoch.
Návod: Musíte rozlíšiť prípady , a pre nejaké . Uvedomte si, že a .