forum-msg-id-124664-edit

Original date:2014-06-20 11:21:17 Edited by: _tom Subject: Re: Discuss changing the behaviour of append()

Here is some code to test append:

-- given 
sequence s = {1,2,3,4,5,6,7,8,9,10} 
 
-- want 
--> {2,4,6,8,10}  that is reduce s to all even items 
--> use a recusive function to do this 
 
function isEven(atom x) 
    --common utility function 
    return remainder( x,2 ) = 0 
end function 
 
--------------------------------------------------------------- 
 
-- CASE I 
 
 
function extractEvens1( sequence list ) 
   if equal( list, {}) then 
      return {} 
    elsif isEven( list[1] ) then 
       return append( list[1], extractEvens1( list[2..$ ] ) ) 
    else 
        return extractEvens1( list[2..$] ) 
    end if 
end function  
 
? extractEvens1( s ) 
 
--error 
--> first argument of append must be a sequence  
 
 
 
-- CASE II 
 
-- respond to classic OE error message 
-- use &   
-- (since append can not be used) 
 
function extractEvens2( sequence list ) 
   if equal( list, {}) then 
      return {} 
    elsif isEven( list[1] ) then 
       return  list[1] &  extractEvens2( list[2..$ ] ) 
    else 
        return extractEvens2( list[2..$]) 
    end if 
end function  
 
? extractEvens2( s ) 
--> {2,4,6,8,10} 
--> get the "expected" result 
 
 
 
-- CASE III 
 
-- "change behavior of append" 
-- grab a slice 
 
function extractEvens3( sequence list ) 
   if equal( list, {}) then 
      return {} 
    elsif isEven( list[1] ) then 
       return  append( list[1..1] ,   extractEvens3( list[2..$ ] ) ) 
    else 
        return extractEvens3( list[2..$]) 
    end if 
end function  
 
? extractEvens3( s ) 
 
-- { 
--  2, 
--  { 
--    4, 
--    { 
--      6, 
--      { 
--        8, 
--        {10,{}} 
--      } 
--    } 
--  } 
--} 
 
sequence s3 = extractEvens3(s) 
include std/sequence.e 
s3 = stdseq:flatten(s3) 
? s3 
--> {2,4,6,8,10} 
 
-- CASE IV 
 
function extractEvens4( sequence list ) 
   if equal( list, {}) then 
      return {} 
    elsif isEven( list[1] ) then 
        if atom( list[1] ) then 
            list[1] = { list[1]} 
        end if 
       return  append( list[1] ,   extractEvens2( list[2..$ ] ) ) 
    else 
        return extractEvens4( list[2..$]) 
    end if 
end function  
 
? extractEvens4( s ) 
 
-- { 
--  2, 
--  {4,6,8,10} 
-- } 
-- an odd looking result 
  • CASE I: classic error message, I did something wrong.
  • CASE II: based on classic error message, I fix the program.
  • CASE III: try to "fix" append, I get a result in an indirect way.
  • CASE IV: try to "fix" append, output is now ugly.

The proposed change to O[ looks like

3.1 --- a/source/execute.e	Tue Jun 10 20:31:17 2014 -0400  
     3.2 +++ b/source/execute.e	Sun Jun 15 18:33:11 2014 -0400  
     3.3 @@ -2965,7 +2965,11 @@  
     3.4  	a = Code[pc+1]  
     3.5  	b = Code[pc+2]  
     3.6  	target = Code[pc+3]  
     3.7 +	if atom(val[a]) then  
     3.8 +	val[target] = append({val[a]}, val[b])  
     3.9 +	else  
    3.10  	val[target] = append(val[a], val[b])  
    3.11 +	end if  
    3.12  	pc += 4  
    3.13  end procedure  

Looks like this is a CASE IV solution

  • works for a simple case
  • result can be ugly

Explaining CASE I to newcomers to O[ is simple. Explaining CASE IV is difficult; hard to justify a possible ugly result.

_tom

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu