Commit bdd87cfc authored by Pietro Abate's avatar Pietro Abate
Browse files

[r2004-07-11 00:24:29 by beppe] A page of exercises

Original author: beppe
Date: 2004-07-11 00:24:29+00:00
parent 04d0c331
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<page name="tutorial_exercises">
You can cut and paste the code on this page and
test it on the <a href="cgi-bin/cduce">online interpreter</a>.
<box title="Tree navigation" link="intro">
<section title="XPath expressions">
<p>Write a function that implements <code>//t</code> without
using references types and <code>xtransform</code></p>
<li>Give a non tail-recursive version</li>
<li>Give a tail-recursive version</li>
<box title="Patterns" link="pat">
<section title="Sort (by Artur Miguel Diaz:">
Write a non recursive function of type <code> Int -> Latin1</code> which given a non-negative number produces all its digits in the order.
The function is given below nearly completely programmed. Define the patterns that allows to produce the result.</p>
<sample> <![CDATA[
let sortAlg (n :Int):Latin1 =
match string_of n with
]]> </sample>
<sample> <![CDATA[
fact 200 =
sortAlg (fact 200) =
]]> </sample>
<box title="Solutions" link="intro">
<section title="Tree navigation">
type t = %%specify here a type to test%%
fun ( x :[Any*]):[t*] =
let f( x :[Any*]):[t*]) = ...
<p>Note here that the recursive function <code>f</code> is wrapped by a second anonymous function so that it does not expose the recursion variable.</p>
fun (e : [Any*]):[ T*] =
let f( accu :[T*] , x :[Any*]):[T*] =
match x with
[ h&T&<_>(k&[Any*]) ;t] -> f( accu@[h], k@t)
| [ <_>(k&[Any*]) ;t] -> f( accu, k@t)
| [ h&T ;t] -> f( accu@[h], t)
| [ _ ;t] -> f( accu, t)
| [] -> accu
in f ([], e);;
<p>Note that this implementation may generate empty branch warnings in particular</p>
<li>for the first branch if <code>T&amp;&lt;_>(k&amp;[Any*])</code> is <code>Empty</code></li>
<li>for the second branch if <code>&lt;_>(k&amp;[Any*])</code> is smaller than <code>T&amp;&lt;_>(k&amp;[Any*])</code></li>
<li>for the first branch if <code>t</code> is smaller than <code>&lt;_>(k&amp;[Any*])</code></li>
<section title="Patterns">
<sample> <![CDATA[
let sortAlg (n :Int):Latin1 =
match string_of n with
[ (s0::'0' | s1::'1' | s2::'2' | s3::'3' | s4::'4' |
s5::'5' | s6::'6' | s7::'7' | s8::'8' | s9::'9')+ ] ->
s0 @ s1 @ s2 @ s3 @ s4 @ s5 @ s6 @ s7 @ s8 @ s9
| _ -> raise "Invalid argument for sortAlg."
]]> </sample>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment