ϵ">]>
6. CVIČENIE Z PREDMETU ÚVOD DO DEKLARATÍVNEHO PROGRAMOVANIA
http://ii.fmph.uniba.sk/cl/courses/1-AIN-505-udp/0910ls/ex/ex06.cl
Dátum: 29. a 31. 3. 2010
Odporúčaná verzia CL: 5.81.19
WWW stránka predmetu: http://ii.fmph.uniba.sk/cl/courses/1-AIN-505-udp/0910ls/?lang=sk
Characters and strings. In CL, a string is a list of characters (see the definition of the predicates – formats and in module ). Characters are enclosed in quotes (e.g., ), strings in apostrophes (e.g., ). You can use the format to display a number as a character, and the format to display a list of numbers as a string, for example:
90 = c:Ch & 72,101,108,108,111,33,0 = y:Str & ("l","a",0) = y:Str & 'tra'++y++y = z:Str
Znaky a reťazce. Reťazec je v CL zoznamom znakov (viď definície predikátov – formátov and v module ). Znaky sú uzavreté v úvodzovkách (napr. "a"), reťazce v apostrofoch (napr. 'abc efg 123'). Na zobrazenie čísel ako znakov použite formát , na zobrazenie zoznamov ako reťazcov použite formát , napríklad:
90 = c:Ch & 72,101,108,108,111,33,0 = x:Str & ("l","a",0) = y:Str & 'tra'++y++y = z:Str
Lists of strings. So far, we have work mostly with simple lists of numbers, and occasionally with pairs of lists. In this exercise, most of the assignments deals with lists of lists. It is easier to look at them as lists of strings. We have above define a predicate – format , which holds for lists of strings, and can be used in queries do display the values of variables like that.
Try out the query
(97,98,99,0),(100,101,0),0 = t:Ls
Zoznamy reťazcov. Doteraz sme väčšinou pracovali s jednoduchými zoznamami čísel a občas s dvojicami zoznamov. V tomto cvičení sa väčšina úloh týka zoznamov zoznamov. Jednoduchšie je pozerať sa na ne ako na zoznamy reťazcov. Vyššie sme zadefinovali predikát – formát , ktorý platí pre zoznamy reťazcov a umožňuje takto zobrazovať hodnoty premenných v query.
Vyskúšajte napríklad
(97,98,99,0),(100,101,0),0 = t:Ls
Polymorphic typing of lists. It is of advantage to use dedicated variables to indicate types of lists.
use variables , , , … for elements (characters),
use variables , , , … for lists of elements (strings),
use variables , , for lists of lists of elements (lists of strings).
Typing of some of list operations
and
and
and
and
and
Polymorfné typovanie zoznamov. V nasledujúcich úlohách je výhodné (najmä ak ste to nerobili doteraz) používať vyhradené premenné na označovanie typov:
používajte premenné , , , … pre prvky (znaky),
premenné , , , … pre zoznamy prvkov (reťazce),
premenné , , pre zoznamy zoznamov prvkov (zoznamy reťazcov).
Typovanie niektorých zoznamových operácií
a
a
a
a
a
Assignment 1. Define a function that applies the function to each element of the list . The argument is a parameter of the function . Formally, this is specified as
or semiformally as
Úloha 1. Zadefinujte funkciu , ktorá použije funkciu na každý prvok zoznamu . Argument je parametrom funkcie . Formálne toto zadanie vyjadríme
poloformálne
Assignment 2. As in the previous assignment, define a function satisfying:
semiformally
Recall the convention we introduced at the beginning: , , are characters (numbers), , , are strings (lists of numbers), , , are lists of strings (lists of lists of numbers). Read the formulas following this convention. Which variable should we use to denote the result of ?
Úloha 2. Podobne ako v predošlej úlohe, definujte funkciu spĺňajúcu:
poloformálne
Spomeňte si na konvenciu, ktorú sme zaviedli na začiatku: , , sú znaky (čísla), , , sú reťazce (zoznamy čísel), , , sú zoznamy reťazcov (zoznamy zoznamov čísel). Prečítajte formulky podľa tejto konvencie. Ktorú premennú by sme mali použiť na označenie výsledku ?
Assignment 3. Define a function satisfying:
Use the function from the preceding exercise. A possible test query is
Interleave("$",'abcde') = t:Ls
which should produce .
Assignment 4. Let (podpostupnosť) be a predicate satisfying
Give a clausal definition of the predicate .
Assignment 5. Define a function satisfying:
Use the function from Assignment 2. Try out the query
Subseqlist('Jago') = t:Ls
which should yield a list containing (in any order) the strings
;
, , , ;
, , , , , ;
, , , ; .