Logo
- Appeared in:
- 1967
- Influenced by:
- Influenced:
- Paradigm:
- Typing discipline:
- File extensions:
- .lg
- Versions and implementations (Collapse all | Expand all):
Logo is a functional programming language used mostly for educational purposes.
Elements of syntax:
Inline comments | ; |
---|---|
Variable assignment | make "varname value |
Physical (shallow) equality | = |
If - then | if condition [trueBlock] |
If - then - else | ifelse condition [trueBlock] [falseBlock] |
Loop forever | forever [loopBody] |
While condition do | while condition [loopBody] |
Do until condition | until condition [loopBody] |
Examples:
Hello, World!:
Example for versions UCBLogo 6.0print [Hello, World!]
Fibonacci numbers:
Example for versions UCBLogo 6.0This 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.0This 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.0This example defines function quadratic
which 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
Comments
]]>blog comments powered by Disqus
]]>