Factor 0.94Version of implementation Factor of programming language Factor
Stable release of Factor, released on September 18, 2010.
The first line imports
io dictionary (
USE: io "Hello, World!" print
The first line lists the necessary dictionaries:
math (arithmetical operators) and
Next the definition of
factorial word follows which replaces an integer
n with its factorial on the top of the stack. To do this, it constructs an array of numbers from 0 to
n — 1 (word
iota) and folds it with
1 using quotation
[ 1 + * ] (increment and multiply) and combinator
The main program constructs a list of numbers from 0 to 16 (
iota again) and for each of them (combinator
each) applies the quotation which calculates the factorial and outputs the result in required format.
math.combinatorics contains word
factorial defined exactly like this.
USING: formatting kernel math sequences ; IN: factorial-example : factorial ( n -- n! ) iota 1 [ 1 + * ] reduce ; 17 iota [ dup factorial "%d! = %d\n" printf ] each
This example shows recursive calculation of Fibonacci numbers.
fib calculates the n-th number: if the argument is not greater than 1, it stays on the stack as the return value, otherwise it is replaced with a sum of previous numbers. Word
bi is a short-hand version of
cleave combinator and allows to apply two quotations (in this case calls of
fib for smaller arguments) to the same element of the stack (
USING: formatting kernel math sequences ; IN: fibonacci-example : fib ( n -- fib(n) ) dup 1 > [ [ 1 - fib ] [ 2 - fib ] bi + ] when ; 16 iota [ 1 + fib "%d, " printf ] each "...\n" printf
This example uses a purely recursive approach to factorial calculation. Word
n! on the stack with a side effect: it prints all values of factorial from 0 to
if combinator is applied, the stack holds values of
over replace them with
n!; two latter values are used for printing, and the first one stays on the stack as a return value.
In the main part of the program we need to add
drop to remove
16! from the stack, so that the effect of the program on the stack is
( -- ).
USING: formatting kernel math ; IN: factorial-example : factorial ( n -- n! ) dup 0 = [ 1 ] [ dup dup 1 - factorial * ] if swap over "%d! = %d\n" printf ; 16 factorial drop
quadratic-equation takes coefficients of the equation as input and prints the solution while returning nothing. Note that this word is declared using token
:: instead of
: used in most cases; this means that within it lexically scoped variables can be used, in this case parameters
c as well as local variables
sd bound by
:> operator. Such variables can be loaded on the stack using their names. Words and operators which process lexically scoped variables are available in dictionary
Factor provides a built-in data type for complex numbers; whenever the discriminant is negative, its square root will be of type
complex. In this case complex roots are printed using words
imaginary-part which extract corresponding parts of a number.
readln reads a string from input stream (till the end of line), and
math.parser dictionary) converts a string to a floating-point number.
USING: formatting io kernel locals math math.functions math.parser ; IN: quadratic-example :: quadratic-equation ( a b c -- ) a 0 = [ "Not a quadratic equation." printf ] [ b sq a c * 4 * - :> d b neg a 2 * / :> x0 d sqrt a 2 * / :> sd d 0 = [ x0 "x = %f\n" printf ] [ d 0 > [ x0 sd + x0 sd - "x1 = %f\nx2 = %f\n" printf ] [ x0 sd + [ real-part ] [ imaginary-part ] bi "x1 = (%f, %f)\n" printf x0 sd - [ real-part ] [ imaginary-part ] bi "x2 = (%f, %f)\n" printf ] if ] if ] if ; readln string>number readln string>number readln string>number quadratic-equation
This example uses regular expressions. Word
regexp) splits a string into an array of strings separated by matches to the given regular expression. After this
map combinator applies word
unicode.case) to each element of the resulting array, converting them to title case. Finally,
sequences) concatenates the strings into one using “” as a separator.
USING: kernel io regexp sequences unicode.case ; readln R/ [^a-zA-Z]+/ re-split [ >title ] map "" join print
This example uses a built-in word
factorial defined in dictionary
USING: formatting kernel math.combinatorics sequences ; 17 iota [ dup factorial "%d! = %d\n" printf ] each