Ремонт принтеров, сканнеров, факсов и остальной офисной техники


назад Оглавление вперед




[30]

orelse (x-y=x-y) fun conflict(pos, []) = false I conflict(pos, h::t) =

threat(pos, h) orelse conflict(pos, t)

exception Conflict;

fun addqueen(l,n,place) = let fun tryqueen(j) =

( if conflict((i,j), place) then raise Conflict else if i=n then (i,j)::place else addqueen(l+l, n, (i,j)::place) ) handle Conflict =>

if j=n then raise Conflict else tryqueen(j+l) in tryqueen(l) end

fun queens(n) = addqueen(l, n, []) Ответ 2.8.3:

exception Conflict of ((int*int) list) list

fun addqueen(l,n,place,places) = let fun tryqueen(j, places) =

( if conflict ((i,j), place) then raise Conflict(((i,j)::place)::places) else addqueen (1+1, n, (ij)::place, places) ) handle Conflict newplace =>

if j=n then raise Conflict(newplace) else tryqueen(j+1.newplaces) in tryqueen(l.places) end

fun allqueens(n) = addqueen(l, n, [], [])

handle Conflict(places) => places

Ответ 2.9.1:

val primes =

let fun nextprime(n,l) =

let fun check(n,[]) = n

I check(n,h::t) = if (n mod h) = 0 then check(n+l,l) else check(n.t)

in check(n.l) end fun primstream(n,l) =


mkstream (fn () =>

let val n=nextprime(n,l) in (n, primstream(n+l,n::1)) end) in primstream(2, []) end

Ответ 2.9.2:

abstype a stream =

stream of ( unit -> (a * a stream)) ref with fun next(stream f) =

let val res = (!f) () in (f := fn() => res; res) end fun mkstream f = stream(ref f)

Приведем другое решение - более многословное, но, возможно, более понятное:

abstype a stream = stream of a streamelmt ref

and a streamelmt = uneval of (unit -> (a*a stream)) I eval of a*a stream with fun next(stream(r as ref(uneval(f)))) =

let val res=f() in (r := eval res; res ) end I next ( stream (ref(eval(r)))) = r fun mkstream f = stream(ref(uneval f))

Ответ 2.9.3:

abstype a stream =

stream of (unit -> (a * a stream)) ref with local exception EndOfStream in fun next(stream f) = let val res = (!f)() in (f := fn () => res; res) end

fun mkstream f = stream (ref f) fun emptystreamO =

stream(ref (fn () => raise EndOfStream)) fun endofstream(s) =

(next s; false) handle endofstream => true


structure INTORD : ORD = struct

type t = int

val le : int*int -> bool = op < end

structure RSORD : ORD = struct

type t = real*string

fun le((rl:real, si:string), (r2,s2)) =

(rl < r2) oresle ((rl = r2) andalso (si < s2)) end

Ответ 3.2.2:

Сигнатура указывает, что тип п есть a list; отсюда, в частности, следует, что если структура Т сопоставима с сигнатурой SIG, то выражение true: :(Т,п) должно быть допустимым. Это условие не будет выполнено в нашем примере, поскольку п имеет более конкретный тип, а именно int list. Поэтому данное определение компилятор признает ошибочным.

Ответ 3.2.3:

sig type a t val х : bool*int end

sig type a t vai x : bool t end

Ответ 3.2.4:

Только сигнатура В удовлетворяет правилу замкнутости сигнатуры (все остальные содержат открытые ссылки на структуру А).

Ответ 3.2.5:

signature STACK = sig

datatype a stack = nilstack I push of a * a stack exception pop and top val empty : a stack -> bool and pop : a stack -> a stack and top : a stack -> a end



[стр.Начало] [стр.1] [стр.2] [стр.3] [стр.4] [стр.5] [стр.6] [стр.7] [стр.8] [стр.9] [стр.10] [стр.11] [стр.12] [стр.13] [стр.14] [стр.15] [стр.16] [стр.17] [стр.18] [стр.19] [стр.20] [стр.21] [стр.22] [стр.23] [стр.24] [стр.25] [стр.26] [стр.27] [стр.28] [стр.29] [стр.30] [стр.31] [стр.32]