Programming Examples


loan.4thMortgage Loan Payment Calculator
lf.4thA Simple Word Processor by Leo Wong
notes.4thSimple notes database
poetry-reader.4thDisplay Poems From Sequentially Numbered Text Files (screenshot, sample poem files)
speech.4thInterface to the Festival Text To Speech System

Games and AI

magic.4thMagic Squares
hanoi.4thTowers of Hanoi puzzle
tetris.4thTetris by Dirk Uwe Zoller
corewars.4thBattling Programs
pentomino.4thSolutions to the Pentomino Puzzle by Bruce Hoyt
sudoku.4thSudoku Puzzle Solver by Robert Spykerman
life.4thConway's Game of Life by Leo Wong
animal.4thAnimal Guessing Game by Ed Beroset
eliza.4thEliza psychoanalysis game
gps.4thGeneral Problem Solver (AI example)
shrdlu.4thSon of SHRDLU AI game by Marcel Hendrix
strek.4thSuper Star Trek by Mike Mayfield and others
tscp.4thTSCP Tom's Simple Chess Program by Ian Osgood
tscp-tgfx.4thTSCP Chess Program with Text Graphics (screenshot)
chessboard.4thText Graphics Chessboard
backprop.4thBackpropagation Neural Network Module by Marcel Hendrix
ocr.4thOptical Character Recognition Example of Neural Net by Marcel Hendrix
react.4thVisual Reaction Time Measurement
keycal.4thKey Event Processing Time Measurement

Programming Tools and Libraries

ans-words.4thAdditional ANS Forth Words for kForth
files.4thFile Access (x86 Linux version)
strings.4thString Manipulation/Number Conversion
ansi.4thANSI Terminal Control
dump.4thMemory Display (provides DUMP)
utils.4thUseful Words for kForth
struct-200x.4thForth 200x data structures
struct-200x-ext.4thUnaligned fields for struct-200x.4th
macro.4thMacros in Forth
mini-oof.4thBernd Paysan's Object-Oriented Extensions to Forth (adapted for kForth)
mini-oof-demo.4thDemonstrate Object-Oriented Programming Using mini-oof
lists.4thLisp like List Processing
lists-test.4thTest List Words
ftran202.4thFORmula TRANslator by J.V. Noble (see ftrandoc.txt)
ftran-test.4thTest FTRAN
asm-x86.4thAn assembler for x86 Processors
asm-x86-examples.4thAssembly Code Examples for asm-x86
fcalls-x86.4thForth to C Function Calling Interface
lib-interface.4thInterface to Functions in an External Shared Object Library
syscalls.4thLinux System Calls from Forth
syscalls.4thWin32 System Calls from Forth
parallel-mm.4thExample of Using Linux System Calls and Parallel Computing
socket.4thInterface to Sockets in Linux
server.4thSimple Example of a Network Server
client.4thSimple Example of a Network Client
fpu-x86.4thSet Precision, Rounding Mode, and Exception Controls on x86 FPUs
signal.4thSignals Interface
signals-ex.4thExample of Signal Handling
spinlock-ex.4thExample of Mutual Exclusion via Spinlock (x86)
textbox.4thColor Boxes Using Text Graphics
catch-throw-ex.4thExample of Using CATCH and THROW in Forth
getline.4thEnhanced version of READ-LINE by Bill McCarthy
chr_tbl.4thAutomatic Character Encoding Tables by J.V. Noble
fsm2.4thFinite State Machines in Forth by J.V. Noble
fsm-example.4thExample of Finite State Machine in Forth by J.V. Noble
user.4thDetermine User Properties (Linux)
modules.4thModules Library (kForth version)
modular-forth.lyxSource for modules.4th (standard version)
literate-included.fsInclude Literate Forth Programs Directly From Noweb and LyX Files
literate-included.lyxSource for literate-included.fs

Library Interfaces

x11 interface to the C library for X11 Programming
blas interface to the Fortran BLAS library
gmp interface to the C GNU multi-precision arithmetic library
mpfr interface to the GNU Multi-precision floating point with correct rounding library
cfitsio Interface to the C FITS file i/o library


simple-graphics-x11.4th Module for Generating Simple Graphics Output in an X Window
simple-frames-x11.4th Framework for Generating Interactive, User-Navigable Graphics Presentations and Programs (see examples, henon-graphics.4th, and poetry-reader.4th)
simple-fonts-x11.4thFont Loading and Selection for X11
simple-typeset-x11.4thTypesetting Text in an X Window
font-strings-x11.4thUtility for Making Valid X11 Font Strings
cs-strings-x11.4thSupport for Strings with Embedded Control Sequences
hello-world-x11.4thThe Hello World Program for simple-frames-x11 (screen shot)
typeset-test-x11.4thVarious Text Typesetting Examples in an X Window
banner-x11.4th An animated banner using simple-frames-x11
demo-xft-telugu.4thTelugu Alphabet Using Xft Library ( picture)

System Test Code

ttester.4thGeneral Test Harness by J. Hayes and A. Ertl
tester.4thUtility Words Needed by core.4th
core.4thTest Suite for ANS Forth Core Words by John Hayes
coreplus.4thMore tests for ANS Forth Core Words by Gerry Jackson and Reinhold Straub
stringtest.4thTests for ANS Forth String Words by Gerry Jackson
filetest.4thTests for ANS Forth File Access Words by Gerry Jackson
searchordertest.4thTests for ANS Forth Search Order Words by Gerry Jackson
dbltest.4thTests for ANS Forth Double Words
divtest.4thTests for ANS Forth Division Words and kForth Extensions by David N. Williams
fatan2-test.4thTests for conformance of FATAN2 to ISO C Specification by David N. Williams
fpzero-test.4thTests for IEEE 754 Compliance for Operations with Floating Point Signed Zero
ieee-arith-test.4thTests for IEEE 754 Special Data Arithmetic (INF, NAN, etc) by David N. Williams
ieee-fprox-test.4thTests for IEEE 754 Compliance for Comparisons with Special Data by David N. Williams
paranoia.4thFloating Point Arithmetic Test Program by William Kahan
to-float-test.4thTests for ANS Forth Compliance of >FLOAT by Ed
regress.4thRegression Tests for kForth
asm-x86-test.4thTests for the asm-x86 Assembler for kForth

Data Encoding and Conversion

crc-32.4th32-bit CRC Calculation by Wil Baden
crc-32-x86.4th32-bit CRC Calculation in Assembly Language by Petrus Prawirodidjojo
forth2html.4thCreate HTML pages from Forth Source Files
dos2unix.4thDOS Text to Unix Text File Conversion
lz77.4thLZ77 Data Compression Algorithm by Wil Baden
arcfour.4thARCFOUR Data Encryption/Decryption Algorithm by Neil Bawd
base64.4thBase64 Content Transfer Encoding/Decoding by Wil Baden
hmac-md5.4thMD5 Calculation by Jabari Zakiya
rawfloat.4thConvert Between IEEE Floating-Point Number Formats, by David N. Williams
rawfloat-test.4thTester for rawfloat.4th, by David N. Williams

Interfacing and Data Acquisition

serial.4thSerial Port Interface Module (x86/x86_64 Linux)
serial-comm.4thHigh-level Serial Interface Module
dummy-comm.4thDummy Communications Module
terminal.4thGeneric Terminal Module for Serial Communications (x86/x86_64 Linux)
term.4thTerminal Program with Pluggable Comm Modules
hexterm.4thDisplay Incoming Serial Port Data (Linux)
ioctl.4thGenerate IOCTL numbers under Linux


primes.4thPrime Numbers
benchpin.4thCalculate number of primes less than or equal to n
pispigot.4thDisplay PI to arbitrary number of digits by Albert van der Horst
interp.4thLinear Interpolation with Integer Tables by Chris Jakeman (picture)

Scientific Library

This library provides modules for some commonly used algorithms in scientific computing. Many of the modules have been ported to kForth from the Forth Scientific Library (FSL). The original FSL modules were written for Forth systems which use a separate floating point stack. The kForth versions of these modules, in addition to being modified for a unified data/fp stack, have also been revised and improved in some aspects, most notably with regard to the test code.

FSL Auxiliary Files

fsl-util.4thFSL Definitions of Arrays, Matrices, and other Utilities
fsl-test-utils.4th Utility Words for Automated Testing of the FSL Modules
fsl-tester.4th Automated Tests of Selected FSL Modules
dynmem.4thFSL Routines for Dynamic Memory Use

Complex Numbers

complex.4thFSL Complex Arithmetic and Function Words by J.V. Noble and D. N. Williams
complex-test.4thFSL Test words in complex.4th by David N. Williams

Finding Roots

quadratic.4thFSL Solve Quadratic Equation with Real Coefficients
cubic.4thFSL Cubic Equation Root Solver
lagroots.4thFSL Roots of Polynomials with Complex Coefficients
regfalsi.4thFSL Function Root Finder

Special Functions

expint.4thFSL Real Exponential Integral
elip.4thFSL Complete Elliptic Integral
elip12.4thFSL Complete Elliptic Integrals of First and Second Kind
logistic.4thFSL Logistic Function and its First Derivative
horner.4thFSL Polynomial Evaluation by Horner's Method
polys.4thFSL Evaluate Special Polynomials: Chebyschev, Hermite, Laguerre, generalized Laguerre, Legendre, and Reverse Bessel
jairy.4thAiry Function and its Derivative
bes-jy.4thBessel Functions of First and Second Kind: J_0, J_1, Y_0, Y_1
sph_bes.4thFSL Regular Spherical Bessel Functions (j_0 through j_9)
factorl.4thFSL Factorial Function
gamma.4thFSL Gamma, LogGamma, and Reciprocal Gamma Functions
gamma-aph.4thHigh Accuracy Gamma Function
pcylfun.4thFSL Parabolic Cylinder Functions and Related Confluent Hypergeometric Functions
erf.4thFSL Error Function and Complementary Error Function (relative error table)
erf-wc.4thError Function and Related Functions
zerf.4thComplex Error Function and Complementary Error Function
zwofz.4thFaddeeva Function (reference values)
voigt.4thVoigt Function, and Gaussian and Lorentzian Distributions
zzeta.4thRiemann Zeta Function (Real and Complex)

Power Series

shanks.4thFSL Nonlinear Transformation of Series


derivative.4thNumerical Derivative of x,y Data


adaptint.4thFSL Adaptive Integration Using Trapezoid Rule
gauleg.4thFSL Gauss-Legendre Integration of Functions

Solution of Ordinary Differential Equations

runge4.4thFSL Runge-Kutta Routines for Integrating Ordinary Differential Equations
lorenz.4thDemonstrate Use of FSL runge4.4th with Lorenz Differential Equations (picture)
numerov.4thNumerov Integrator for Second Order Equations
numerov_x86.4thNumerov Integrator in Assembly Language
numerov-test.4thTest Code for Numerov Integrator (see error plot)

Solution of Linear Systems

gaussj.4thFSL Linear System of Equations Solver Using Gauss-Jordan Elimination
lufact.4thFSL LU Factorization of a Square Real Matrix
dets.4thFSL Determinant of an LU Factored Matrix
backsub.4thFSL Linear System of Equations Solver Using LU Factorization
invm.4thFSL Inverse of an LU Factored Matrix
hilbert.4thFSL Finite Segments of Hilbert Matrices, their Inverses, and Determinants
mmul.4thMatrix multiplication (see also parallel-mm.4th)
mmul_x86.4thFast matrix multiplication
determ.4thDeterminant of a Square Matrix
eigen22.4thEigenvalues and Eigenvectors of a Real 2x2 Matrix
zmatrix.4thComplex Floating Point Matrices
zeigen22h.4thCompute Eigenvalues and Eigenvectors of a 2x2 Hermitian Matrix

Curve Fitting

polyfit.4thLeast-Squares Polynomial Fitting (example of line fitting; example of polynomial fitting)
curvefit.4thNon-linear Curve Fitting
gfit-curvefit.4thDemonstrate Use of curvefit.4th ( picture), (see also gfit-levmarq.4th)
func_Ngauss.4thCalculate sum of N Gaussian peaks and baseline
wfms01-1.datSample Data File for Curve Fitting Example
levmarq.4thFSL Levenberg-Marquardt Nonlinear Curve Fitting
gfit-levmarq.4thDemonstrate Use of FSL levmarq.4th


polrat.4thFSL Polynomial and Rational Function Interpolation and Extrapolation
aitken.4thFSL Lagrange Polynomial Interpolation
hermite.4thFSL Hermite Polynomial Interpolation
interp-array.4thLinear Interpolation of FSL Arrays at Specified Abscissas


dfourier.4thFSL Discrete Fourier Transforms
four1.4thFast Fourier Transform (one dimension)
four1-test.4thFFT Example of Square Pulse
fft-x86.4th32-bit Assembly Code FFT (callable from Forth)

Probability and Statistics

permcomb.4thFSL Permutations and Combinations
gauss.4thFSL Gaussian Probability Function
histogram.4thBinning Data

Random Number Generators

isaac.4thFSL ISAAC Random Number Generator
prng.4thFSL GGUBS Floating Point Random Number Generator
ran4.4thFSL RAN4 Random Number Generator (Doubles)
noise.4thUniform and Gaussian Random Number Generation
random.4thAssorted Simple Random Number Generators
ran-next.4thDonald Knuth's Recommended Random Number Generator
mersenne.4thMersenne Twister (2002) Random Number Generator

Clebsch-Gordan Coefficients

cg.4thWigner 3-j Symbols and Clebsch-Gordan Coefficients
cg-test.4thTests for cg.4th

Astronomical Calculations

jd.4thJulian Day and Calendar Calculator
sun.4thSunrise and Sunset Calculator by Neal Bridges

Miscellaneous Utilities

array-utils0.4thLevel 0 Utilities for FSL Arrays and Matrices (incomplete)
array-utils1.4thLevel 1 Utilities for FSL Arrays and Matrices (incomplete)
find.4thFind values in an FP Array
read_xyfile.4thRead Two Column Files into FP Arrays
shellsrt.4thFSL Shell-Metzger Sorting for FP Array
qsort.4thQuicksort Algorithm for Sorting for Integer (Cell) Arrays
crc.4thFSL 16-bit CRC (Cyclic Redundancy Check) Calculation by Gordon Charlton
machar.4thDetermine floating point machine charactersitics by David N. Williams


struct.4thData structure words (deprecated in favor of Forth 200x structures)
struct-ext.4thData structure extension words (replace with unofficial Forth 200x structure extensions)

Science Demos

atrial-fib.4thCellular Automata Model for Atrial Fibrillation (see Physics Focus)
grating.4thCompute Diffraction Grating Properties
pde1.4thNumerical Solution of Electrostatics Boundary Value Problems (sample solution)
pde2.4thNumerical Solution of the 1-D Diffusion Equation
sl.4thSemiconductor Laser Rate Equation Solver ( instructions)
lk.4thLang-Kobayashi Equation Solver (picture)
qm4.4thQuantum Mechanics Demonstrations: Properties of Eigenfunctions and Operators
qm8.4thQuantum Mechanics Demonstrations: Bloch Equations, Rabi Oscillations, and Inversion of a Two-Level System
schr1d.4thNumerically Solve the Radial Schroedinger Equation
potential.4thModule for reading and interpolating a potential energy curve
hf.4thModule for Hartree-Fock calculations (incomplete)
H-atom.4thFind the radial functions and energies of the hydrogen atom by direct numerical integration
he-scf.4thFind the radial function(s) and ground-state energy of the helium atom
by the self-consistent field method.
iso_1s2_hf.4thCompute atomic properties of the isoelectronic sequence for electron configuration
1s(2) for Z = 2 to 10.
h2XJ0.4thExample of Using schr1d.4th to Find Vibrational Levels of H_2 Molecule (requires H2-potnl-pac2010.dat)
h2XJ0-x11.4thGraphical Display of Vibrational Levels and Probability Densities of the H_2 Molecule (picture)
bifurc-logmap.4thBifurcation Diagram for the Logistic Map (picture)

Source examples for kForth embedded inside of the application xyplot are provided here.