1. Re: ABS & recursion

>function call overhead... having to push and pop all those
>newly created/destroyed local duplicate variables is simply
>a clock-tick eater and there ain't much can be done...
>recursion without pushing/popping? then you have something.
>and euphoria would really shine then...
>kinda makes ya wonder what ol' wirth is up to indeed?


When a function calls itself (recursion) cant Euphoria inline a sort of new
non-recursive algorithm in their..  most compilers have an option for
end-tail-recursion, why not Euphoria ?

Off course many of the push and pop overhead remains, however, not all
arguments, declarations, etc. have to be done everytime, esspecially because
they are allready done. I dont exactly know what Im talking about, but I
figure you can emulate recursion:

function bla (sequence s)
    if length(s) then
        s[1] = s[1] + 1
        return s[1] & bla(s[2..length(s)])
    else
        return s
    end if
end function

-- That was a recursive one..
-- This one does preciously the same:

function bla (sequence s)
    for index = 1 to length(s)
        s[index] = s[index] + 1
    end for
    return s
end function

-- And now one using EUphoria's auto-recursion (which off course must
*never* be slower than recursion programmed in Euphoria code, as with the
ABS function timings!)

function bla (sequence s)
    return s + 1
end function

My point here was, we've been trying to manage to do ABS (for example) with
real-recursion, the auto-recursive operands, but we can also try to do it
without recursion:
This example is tested.. not just writing inside my mailer!

Its a recursive ABS function, that doesnt do any function call at all
(except for a few built-in) and it doesnt use any auto-recursive operators.

-- An demonstration of how to simulate recursion....
-- I havent timed it, but take my word for it, its * SLOW *
-- Have with it ya'all... just remember standard disclaimer applies..

-- Ralf Nieuwenhuijsen (nieuwen at xs4all.nl)

function abs (object x)
  sequence hold
  object subject
  integer flag, start

  if atom(x) then
    if x < 0 then return -x else return x end if
  end if
  hold = {}
  start = 1
  while 1 do
    flag = 1
    for index = start to length(x) do
      subject = x[index]
      if atom(subject) then
        if subject < 0 then x[index] = -subject end if
      else
        hold = append(hold, {x, index})
        x = x[index]
        flag = 0
        start = 1
        exit
      end if
    end for
    if flag then
        if length(hold) then
            start = hold[length(hold)][2]
            hold[length(hold)][1][start] = x
            start = start + 1
            x = hold[length(hold)][1]
            hold = hold[1..length(hold)-1]
        else
            return x
        end if
    end if
  end while
end function

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu