<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
<br>I have to show my class an array bubblesort. I refused to show them do - let the imperative guy introduce them to that evil stuff - methinks. So heres my effort which my guts tell me is not the best and I'm sure at some point the design recipe went out the window (first time I've fallen off the wagon).<div><br></div><div><div><div>(define (bubble-sort array)</div><div>&nbsp;&nbsp;(local [(define any-changes? true)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(define (sink-the-largest starting-idx ending-idx) &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(if (= starting-idx ending-idx)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(bubble-sort ending-idx)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(begin</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(local [(define current-value (vector-ref array starting-idx))</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(define next-value (vector-ref array (add1 starting-idx)))] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(when (&gt; current-value next-value) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(begin</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(vector-set! array starting-idx next-value)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(vector-set! array (add1 starting-idx) current-value)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(set! any-changes? true))))</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(sink-the-largest (add1 starting-idx) ending-idx)))) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(define (bubble-sort ending-idx) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(if any-changes?&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(begin</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(set! any-changes? false)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(sink-the-largest 0 (sub1 ending-idx))) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array))]</div><div>&nbsp;&nbsp; &nbsp;(bubble-sort (vector-length array))))</div><div><br></div></div><div>(bubble-sort (vector 92 4 2 32 92 18 16 34 16 32 10))</div><div><br></div><div>Then I start thinking. The students will almost inevitably have to write some imperative code but some of them have figured out and bought into my strategy - use Scheme wherever you can - because it's "easier" to write. Taking that one step further I thought heck - if teach them do they might be able to do all their coding in Scheme (if they want to).</div><div><br></div><div>Given that I had just convinced them (I think) why assignment should only be used where necessary my misgivings were mitigated by this passage from Dyvbig's book&nbsp;</div><div><br></div><div><span class="Apple-style-span" style="font-family: 'Times New Roman'; font-size: 12pt; ">"Although looping constructs in most languages require that the loop iterands be updated via assignment,&nbsp;<tt>do</tt>&nbsp;requires the loop iterands&nbsp;<tt><i>val</i>&nbsp;...</tt>&nbsp;to be updated via rebinding. In fact, no side effects are involved in the evaluation of a&nbsp;<tt>do</tt>&nbsp;expression unless they are performed explicitly by its subexpressions."</span></div><div><br></div><div>My effort with do is also nicer shorter and clearer</div><div><br></div><div><div>(define (bubble-sort array)</div><div>&nbsp;&nbsp;(local ([define end (sub1 (vector-length array))]</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[define any-changes? false]) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;(do [(i 0 (add1 i))] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp;((= i end) (if any-changes? (bubble-sort array) array))</div><div>&nbsp;&nbsp; &nbsp; &nbsp;(local ([define current (vector-ref array i)]</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[define next (vector-ref array (add1 i))])</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;(when (&gt; current next)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(set! any-changes? true)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(vector-set! array (add1 i) (max current next))</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(vector-set! array i (min current next)))))))</div></div><div><br></div><div>I'd welcome improvements on my doless effort (no HOF's - it's beyond the scope of what I am teaching) that I could also show them.</div><div>&nbsp;</div><div><br></div><div><br></div><br></div>                                               <br /><hr />Do you want a Hotmail account? <a href='http://clk.atdmt.com/UKM/go/197222280/direct/01/' target='_new'>Sign-up now - Free</a></body>
</html>