15.08.2013 Views

General Computer Science; Problems and Solutions for ... - Kwarc

General Computer Science; Problems and Solutions for ... - Kwarc

General Computer Science; Problems and Solutions for ... - Kwarc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Problem 1.14: Program the function f with f(x) = x 2 on unary natural numbers without<br />

using the multiplication function.<br />

Solution: We will use the abstract data type mynat<br />

datatype mynat = zero | s of mynat<br />

fun add(n,zero) = n | add(n,s(m))=s(add(n,m))<br />

fun sq(zero)=zero|sq(s(n))=s(add(add(sq(n),n),n))<br />

15<br />

15pt

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!