Original date:2015-03-31 06:12:06 Edited by: Shian_Lee Subject: Re: NULL value anyone?

First of all Tom, please accept my apology.
I did not mean to underestimate you in any way. I was rude and it's not the first time.
Your work here is extremely important. Without good documentation the languages is totally useless.

About the stack example:

_tom said...

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 
        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" ) 
end if 
display( "               [1]", {sk} ) 
{sk,x} = pop(sk) 
if atom(sk) then display( "Underflow" ) 
end if 
display( "               [1]", {sk} ) 

Could be modified, for example, to (not debugged):

-- it's faster to call  append(stack, x)  directly. 
function push(sequence stack, object x) 
    return append(stack, x) 
end function 
function pop(sequence stack) 
    integer len = length(stack) 
    if len then      
        return {stack[1..len - 1], stack[len]} 
        return {} -- return stack to its initial state. 
    end if 
end function 
-- Example for modified push()/pop(): 
sequence stack = {} 
object   item = "Shian is rude" 
stack = push(stack, item)    --> or faster:  append(stack, item) 
stack = pop(stack) 
if length(stack) then       
    stack = stack[1] 
    item = stack[2] 
end if 

About using Booleans... Normally I use stacks in-line as a global/local/private sequence, for specific purpose, so in this case I can optimize the process by using an integer as a length counter, to avoid calling length(). Booleans can be used in many ways to optimize the code, for example: stack1_empty = not length(stack[1][4])
If I must test for an empty stack many times then it's better to code: if stack1_empty then...
instead of coding if not length(stack[1][4]) then...

There are many other ways to use Booleans, depends on what the stack is for. Booleans can be stored in a single integer and set/reset using or_bits()/and_bits() which is fast and convenient.

The important thing to remember is that NOVALUE modifies the behavior of Euphoria in a massive way, and the "Cost Vs Benefit" might be awful and truly not reasonable.

Not Categorized, Please Help


Quick Links

User menu

Not signed in.

Misc Menu