Note that there are some explanatory texts on larger screens.

plurals
  1. POPorts and Cells in Oz
    text
    copied!<p>I'm taking a teach-yourself course at the university at the moment and noticed I bitten off a bit more than I can chew. The course is about the Oz programming language and I'm reading an e-book about it to try to get to know it better and I'm trying to solve some exercises to test my understanding. I'm quite stuck at a certain exercise and I don't quite know how to solve it. The question goes:</p> <blockquote> <p>Implementing ports. In Chapter 5 we introduced the concept of port, which is a simple communication channel. Ports have the operations {NewPort S P}, which returns a port P with stream S, and {Send P X}, which sends message X on port P. From these operations, it is clear that ports are a stateful unbundled ADT. For this exercise, implement ports in terms of cells, using the techniques of Section 6.4.</p> </blockquote> <p>The key words are stateful unbundled and cells. I tried to implement the behavior of ports using cells with the following approach:</p> <pre><code>declare MyPort MyStream proc {NewPortEx ?S ?P} P = {NewCell nil} S = !!P %read only view on the cell end proc {SendEx P X} P:=X|@P %extend the cell's content, a list, with X end in {NewPortEx MyStream MyPort} {Browse @MyStream} {SendEx MyPort c} {Browse @MyStream} </code></pre> <p>My final step would be to add a wrapper/unwrapper pair to make the ADT secure but first I want the functionality to work properly. This seems to do the right behavior but it's not as I had wanted it. I would like to be able to call <code>{Browse MyStream}</code>, without the <code>@</code>, just once. I was hoping the browser would display something along the lines of <code>firstSent|secondSent|_&lt;future&gt;</code> instead it displays <code>&lt;Cell&gt;</code> as is to be expected and I need to call <code>Browse</code> to it after each <code>Send</code>, and the output is shown as a list: <code>[firstSent secondSent]</code>.</p> <p>If I remember correctly from the theory I read, this has to do with eager vs lazy evaluation, respectively resulting in (finite) lists vs (infinite) streams.</p> <p>I have the feeling I'm not doing it quite right though and I have no experience whatsoever with functional languages, can anyone help me out with making an example of a port implemented with cells? (basically your own implementation of the existing <code>NewPort</code> and <code>Send</code>)</p> <p>Thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload