UCBLogo

Implementation of programming language Logo

UCBLogo, also referred to as Berkeley Logo, is a GPL Logo interpreter.

Examples:

Hello, World!:

Example for versions UCBLogo 6.0
print [Hello, World!]

Fibonacci numbers:

Example for versions UCBLogo 6.0

This example uses recursive definition of Fibonacci numbers. It defines two functions — fibonacci which calculates the value of Nth Fibonacci number and print_fibonacci which accumulates the numbers in a string and prints them.

to fibonacci :N
   ifelse :N < 3 [output 1] [output sum fibonacci :N - 1 fibonacci :N - 2]
end

to print_fibonacci :i :N
   make "str fibonacci :i
   make "i sum :i 1
   make "comma ",
   repeat :N - :i + 1 [make "str (word :str :comma fibonacci :i)
                      make "i sum :i 1]
   make "str word str ",...
   print str
end

print_fibonacci 1 16

Factorial:

Example for versions UCBLogo 6.0

This example uses recursive factorial definition. It defines two functions — factorial which calculates N! and print_factorial which loops through numbers from i to N and outputs their factorials.

to factorial :N
   ifelse :N = 0 [output 1] [output :N * factorial :N - 1]
end

to print_factorial :i :N
   repeat :N - :i + 1 [(print :i [! =] factorial :i)
                       make "i sum :i 1]
end

print_factorial 0 16

Quadratic equation:

Example for versions UCBLogo 6.0

This example defines function quadraticwhich accepts coefficients of quadratic equation and prints the roots. Usage is quadratic 1 -2 1.

to quadratic :A :B :C
   if :A = 0 [(print [Not a quadratic equation.])
              stop
             ]
   make "D :B*:B - 4*:A*:C
   if :D = 0 [(print [x = ] -:B/2/:A)
              stop
             ]
   if :D > 0 [(print [x1 = ] (-:B+sqrt :D)/2/:A)
              (print [x2 = ] (-:B-sqrt :D)/2/:A)
              stop
             ]
   (print [x1 = (] -:B/2/:A [,] (sqrt (-:D))/2/A [)])
   (print [x2 = (] -:B/2/:A [,] (-sqrt (-:D))/2/A [)])
end