[ Back to the overview Matrix ]

Test case : Prime Numbers using Foo

Lines used: 22
Here you can see that Foo is a glue language, not really (yet?) a programming one. It is slow... Also you can not define classes, objects, ... As a temporary workaround we use the special Agent class. Each agent can have its own set of methods and has a memory.
  
('cache = '(2))

(&fix message divides (x)
  ((x % self) == 0))

(&fix message prime? ()
  ((1 == self) if
    true
    ((self <= (cache last)) if
      (cache contains self)
      (((cache separate '<= (self sqrt)) any 'divides self) if
        false
        (? last
           (cache +! self)
           true)))))

('source = ({agent} new))
(source learn "n" 1)
((source category) message next-prime ()
  ('n = (self remember "n"))
  ('n ++)
  ('(n prime?) until ('n ++))
  (self learn "n" n)
  n)

(? . (10000 times (source next-prime)))
Contributed by Guillaume, google at memoire.com