Ascend FD-23R Manual do Utilizador Página 61

  • Descarregar
  • Adicionar aos meus manuais
  • Imprimir
  • Página
    / 81
  • Índice
  • MARCADORES
  • Avaliado. / 5. Com base em avaliações de clientes
Vista de página 60
2.5. Lazy Constraint Programs 49
%%%%%%%%%% NOW WE GENERATE AN INFINITE LIST OF LISTS
%%%%%%%%%% CONTAINING THE MAGIC SEQUENCES FROM A NUMBER N
%%%%%%%%%% I.E., THE MAGIC SEQUENCES FROM N,N+1,N+2, ETC.
magicfrom :: int -> [[int]]
magicfrom N = [lazymagic N |magicfrom(N+1)]
Now it is easy to generate a list of N-magic series. For example, the following
goal generates a 3-element list containing, respectively, the solution to the problems of
7-magic, 8-magic and 9-magic series
6
.
TOY(FD)> take 3 (magicfrom 7) == L
yes
L == [ [ 3, 2, 1, 1, 0, 0, 0 ], [ 4, 2, 1, 0, 1, 0, 0, 0 ],
[ 5, 2, 1, 0, 0, 1, 0, 0, 0 ] ]
Elapsed time: 20 ms.
More expressiveness is shown by mixing curried functions, HO functions, infinite
lists and function composition (another nice feature from the functional component of
TOY(FD)). For example, consider the TOY(FD) code below: :
from :: int -> [int]
from N = [N|from (N+1)]
lazyseries :: int -> [[int]]
lazyseries = map lazymagic.from
where the operator ‘.’ defines the composition of functions as follows (again, function
./2 is predefined in the file misc.toy):
(.):: (B -> C) -> (A -> B) -> (A -> C) (F . G) X = F (G X)
Observe that lazyseries curries the comp osition (map lazymagic).from. Then, it is
easy to generate the 3-element list shown above by just typing the goal
TOY(FD) > take 3 (lazyseries 7) == L
yes
L == [ [ 3, 2, 1, 1, 0, 0, 0 ], [ 4, 2, 1, 0, 1, 0, 0, 0 ],
[ 5, 2, 1, 0, 0, 1, 0, 0, 0 ] ]
This goal is equivalent to the following
TOY(FD)> take 3 (map lazymagic (from 7)) == L
6
The function take/2 is predefined in the file misc.toy (see Appendix C).
Vista de página 60
1 2 ... 56 57 58 59 60 61 62 63 64 65 66 ... 80 81

Comentários a estes Manuais

Sem comentários