Object smart routines

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

The following absolute value function can only handle atoms
I will give a demonstration on how to make functions
Object smart.
Object smart means it can handle atoms or sequences.

function absolute(atom a)
  if 0 > a then
    a = -a
  end if
  return a
end function

The function below is object smart.
function abs(object x)
  if atom(x) then
    if 0 > x then
      x = -x
    end if
  else
    for A = 1 to length(x)
      x[A] = abs(x[A])
    end for
  end if
end function

--function use follows
object x

x = absolute(-4)--here x = 4
--x = absolute({5, -4, {}, {{-1}}})--here an error occurred.
x = abs(-4)--here x = 4
x = abs({5, -4, {}, {{-1}}})--here x = {5, 4, {}, {{1}}}

--Lucius Lamar Hilley III
--  E-mail at luciuslhilleyiii at juno.com

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

Search



Quick Links

User menu

Not signed in.

Misc Menu