Re: NULL value anyone?

new topic     » goto parent     » topic index » view thread      » older message » newer message

Here is my NULL rant...

This all started when I looked at the code that makes a stack work:

  • std/stack.e o[4
  • mystack J.Cook
  • stack.e L.Hilley et al

But, reinventing the wheel is more educational.

A stack is a sequence of items. It is a "stack" because you have the discipline to always add (push) and remove (pop) items from the top of the stack.

  • a stack with items: { "cat", "dog" }
  • an empty stack: {}
  • no stack at all: 0

Popping a value from an empty stack is a bad thing--it is an error condition. So I return 0 zero instead. I can check for atoms so I can "catch" this error without crashing.

Testing for atoms vs sequences is an idiomatic Euphoria concept. It does hide the idea that "no stack" has been given the reasonable looking value 0 zero instead of a glaring (here it is idiot!) value like NULL, NOTANUBMER, NAN, FAIL, EXCEPTION which is used in Another_Language.

-- elementry stack push and pop subroutines 
include std/console.e 
 
function push( object stack, object x ) 
    if atom(stack) then  
        return {x}  
        -- new stack from underflow condition 
    end if 
    return append(stack, x ) 
end function 
 
function pop( object stack ) 
    if length(stack) = 0 then      
        return { 0, 0 } 
        -- next pop will be underflow 
    else  
        return { stack[1..$-1], stack[$] } 
    end if 
end function 

A sample run looks like:

object sk = 0 -- new "underflow" stack -- uninitiated 
object x      -- unspecified item 
 
sk = push(sk,"cats") 
sk = push(sk,"dogs") 
display( "               [1]", {sk} ) 
 
     
{sk,x} = pop(sk) 
if atom(sk) then display( "Underflow" ) 
else 
    display(x) 
end if 
display( "               [1]", {sk} ) 
 
{sk,x} = pop(sk) 
if atom(sk) then display( "Underflow" ) 
else 
    display(x) 
end if 
display(sk) 
display( "               [1]", {sk} ) 
Shian_Lee said...

NULL is: Confusing, Complicated.

There is a place for human simplicity.

irv said...

What is two times "darned if I know"? How could that produce meaningful results?

I like Irv's intuition here.

petelomax said...

Looking at the return case in a bit more detail, assuming that a = b

ought to issue a (fatal/recoverable/catchable) error when b is unassigned

Agreed.

I was thinking about a poor man's exception mechanism.

If "b" is uninitialized I can test for it before I try to do an assignment using the object function. I used the word NULL to represent that uninitialized state (maybe there is a better word than NULL). My observation is once you do an assignment you can't undo it. That means I can't use an uninitialized variable as a "trick" to monitor some error condition.

_tom

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu