This is a course page of David Casperson |
|
SK
into a
pure λ-term.
λNfx. Nf(fx)
into an
SKI
combinator.
nth
so that
nth(2,[a,b,c,d],c).
returns true.
Make your rules cope with as many
situations as possible. For instance, it would be nice if your
rules find both
N=0
and
N=2
when given the query.
nth(N,[a,b,a,d],a)
. Comment on the combinations of
variables that rules handle.
N.B., this is builtin as predicate nth0
, and there
are answers
on StackOverflow
and elsewhere for how
to do this. Write your own! (first.)
The goal of this question is to do arithmetic on natural
numbers (0,1,2,…) without using
the Prolog predicates such
as is
. To do this represent the natural numbers as
zero
, s(zero)
, s(s(zero))
,
….
isNat(X)
that is true for
zero
, s(zero)
, s(s(zero))
,
…, and no other ground terms..
xTo(?N,
?X)
that converts between non-negative
integers and isNat
numbers.
portray
, cause
Prolog
to print s(s(zero))
as N(2)
, and so
on.
xPlus(?X,?Y,?Z)
that succeeds when at least two of
X
,
Y
, and
Z
are ground and a solution is possible
[ xPlus(X,s(zero),zero)
should fail].
The idea that you want to exploit is
s(X)+Y=s(Z) iff X+Y=Z.
xTimes(?X,?Y,?Z)
that succeeds when at least two of
X
,
Y
, and
Z
are ground and a solution is possible.
xLessEq(?X,?Y)
that succeeds when
X≤Y
.
xQuoRem(+X,+Y,-Z,-W)
that succeeds when
X=0
or
Y>0
.
Solutions should satisfy
xLessEq(s(W),Y),xTimes(Z,Y,X1),xPlus(X1,W,X)
.
datatype α tree = Empty | Node of α tree * α * α tree ;
Write a function that takes an integer n and produces a nearly balanced tree whose nodes in-order are the integers 1 through n.
α list
and produces a
nearly balanced α tree
whose node values are
taken from the list.
fun fold (f:β * α * β → β) (b:β)
(Empty:α tree) : β = b
| fold (f:β * α * β → β) (b:β)
(Node (l,n,r)) = f(fold f l, n, fold f r)
depth
postorder:α tree → α list
, that
gives a list of the nodes in post-order.
fold
.
fold
that uses
continuations to achieve tail-recursiveness.
datatype α queue = Q of α list * α list ;
Implement the functions
snoc : α * α queue -> α queue ; pop : α queue -> (α * α queue) option isEmpty: α queue -> boolwhere
removesthe front element from a queue, return a pair consisting of the front element and the remainder of the queue. Returns
NONE
if the queue is empty.
How many of these are fair and interesting?
To do this, find the larger root (in absolute value) using the quadratic formula. Find the smaller root using the fact that a r1 r2 = c.
let let
”
in a legal Standard ML program (other than silly ways, such as
in a string constant)?
local local
”
in a legal Standard ML program (other than silly ways, such as
in a string constant)?
fall-2024