Modulinos: Difference between revisions

Line 290:
 
=={{header|Factor}}==
Note: The following INCLUDE/INCLUDING macros must be added to the ~/.factor-rc configuration file.
This "fibonacci" vocabulary exports both a <tt>fib</tt> word (from [[Fibonacci sequence#Factor]]) and a main entry point.
 
Example:
<lang factor>USING: kernel io math math.parser sequences ;
IN: fibonacci
 
<lang sh>$ ./scriptedmain.factor
: fib ( n -- m )
Main: The meaning of life is 42
dup 2 < [
$ ./test.factor
[ 0 1 ] dip [ swap [ + ] keep ] times
Test: The meaning of life is 42</lang>
drop
] unless ;
 
~/.factor-rc:
: first-15 ( -- )
15 iota [
dup
fib number>string write
14 = [ "\n" ] [ ", " ] if write
] each ;
 
<lang factor>! INCLUDE/INCLUDING macros that import source code files in the current directory
MAIN: first-15</lang>
 
USING: kernel vocabs.loader parser sequences lexer vocabs.parser ;
One can run the main program from the listener:
 
IN: syntax
<pre>( scratchpad ) "fibonacci" run
Loading resource:work/fibonacci/fibonacci.factor
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377</pre>
 
: include-vocab ( vocab -- ) dup ".factor" append parse-file append use-vocab ;
One can also run the main program as a script:
 
SYNTAX: INCLUDE: scan-token include-vocab ;
<pre>$ ./factor run=fibonacci
SYNTAX: INCLUDING: ";" [ include-vocab ] each-token ;</lang>
Loading resource:work/fibonacci/fibonacci.factor
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377</pre>
 
scriptedmain.factor:
One can also use the exported words (in the listener, or in another program):
 
<lang factor>#! /usr/bin/env factor
<pre>( scratchpad ) USE: fibonacci
 
( scratchpad ) 20 fib .
USING: io math.parser ;
6765</pre>
IN: scriptedmain
 
: meaning-of-life ( -- n ) 42 ;
 
: main ( -- ) meaning-of-life "Main: The meaning of life is " write number>string print ;
 
MAIN: first-15main</lang>
 
test.factor:
 
<lang factor>#! /usr/bin/env factor
 
INCLUDING: scriptedmain ;
USING: io math.parser ;
IN: test
 
: main ( -- ) meaning-of-life "Test: The meaning of life is " write number>string print ;
 
MAIN: main</lang>
 
=={{header|Haskell}}==
Anonymous user