A hands-on introduction to the Futhark programming language through a collection of commented example programs, covering basic features and programming techniques for parallel computing.
# Futhark by Example
Source: [https://futhark-lang.org/examples.html](https://futhark-lang.org/examples.html)
The following is a hands\-on introduction to Futhark through a collection of commented programs, listed in roughly increasing order of complexity\. You can load the programs into[the interpreter](https://futhark.readthedocs.io/en/stable/man/futhark-repl.html)to experiment with them\. For a conventional introduction to the language,[Parallel Programming in Futhark](https://futhark-book.readthedocs.io/)may be a better choice\. For more examples, you can check our implemented[benchmarks](https://github.com/diku-dk/futhark-benchmarks)\. We also maintain a list of[projects using Futhark](https://futhark-lang.org/examples.html#projects-using-futhark)\.
Some of the example programs use[directives](https://futhark.readthedocs.io/en/latest/man/futhark-literate.html)for plotting or rendering graphics\.
## [Basic language features](https://futhark-lang.org/examples.html#basic-language-features)
- [Basic usage with the factorial function](https://futhark-lang.org/examples/fact.html)
- [Primitive values](https://futhark-lang.org/examples/values.html)
- [Converting a value to a different type](https://futhark-lang.org/examples/converting.html)
- [Mathematical functions](https://futhark-lang.org/examples/math-functions.html)
- [Functions](https://futhark-lang.org/examples/functions.html)
- [Arrays](https://futhark-lang.org/examples/arrays.html)
- [Minimum and maximum](https://futhark-lang.org/examples/minmax.html)
- [Type ascriptions](https://futhark-lang.org/examples/type-ascriptions.html)
- [Basic parallelism](https://futhark-lang.org/examples/basic-parallelism.html)
- [Tuples and records](https://futhark-lang.org/examples/tuples-and-records.html)
- [Size coercions](https://futhark-lang.org/examples/size-coercions.html)
- [Ranges](https://futhark-lang.org/examples/ranges.html)
- [Backtick operators](https://futhark-lang.org/examples/backticks.html)
- [Scans and reductions](https://futhark-lang.org/examples/scan-reduce.html)
- [Parametric polymorphism](https://futhark-lang.org/examples/parametric-polymorphism.html)
- [Polymorphic minimum and maximum](https://futhark-lang.org/examples/polyminmax.html)
- [Gather and scatter](https://futhark-lang.org/examples/gather-and-scatter.html)
- [Exclusive scans](https://futhark-lang.org/examples/exclusive-scan.html)
- [Exclusive prefix sum](https://futhark-lang.org/examples/exclusive-prefix-sum.html)
- [Pipe operators](https://futhark-lang.org/examples/piping.html)
- [Complex ranges](https://futhark-lang.org/examples/complex-ranges.html)
- [Sum types and pattern matching](https://futhark-lang.org/examples/sum-types.html)
- [Loops](https://futhark-lang.org/examples/loops.html)
- [Flattening and unflattening arrays](https://futhark-lang.org/examples/flatten-unflatten.html)
- [In\-place updates](https://futhark-lang.org/examples/inplace.html)
## [Programming techniques](https://futhark-lang.org/examples.html#programming-techniques)
- [Benchmarking](https://futhark-lang.org/examples/benchmarking.html)
- [Counting elements that satisfy property](https://futhark-lang.org/examples/filter-length.html)
- [Index of smallest element \(argmin\)](https://futhark-lang.org/examples/argmin.html)
- [Reducing the result of a filter](https://futhark-lang.org/examples/filter-reduce.html)
- [Scattering the result of a filter](https://futhark-lang.org/examples/filter-scatter.html)
- [Size parameters](https://futhark-lang.org/examples/size-parameters.html)
- [Matrix multiplication](https://futhark-lang.org/examples/matrix-multiplication.html)
- [Pairwise L₁ distances](https://futhark-lang.org/examples/pairwise-l1.html)
- [Outer product](https://futhark-lang.org/examples/outer-product.html)
- [Comparing arrays for equality](https://futhark-lang.org/examples/array-equality.html)
- [Searching](https://futhark-lang.org/examples/searching.html)
- [Option type](https://futhark-lang.org/examples/opt.html)
- [Computing histograms](https://futhark-lang.org/examples/histograms.html)
- [Moving average](https://futhark-lang.org/examples/moving-average.html)
- [Means](https://futhark-lang.org/examples/means.html)
- [Swapping two elements of an array](https://futhark-lang.org/examples/swap.html)
- [Integer logarithm](https://futhark-lang.org/examples/integer-logarithm.html)
- [Radix sort](https://futhark-lang.org/examples/radix-sort.html)
- [Radix sort by key](https://futhark-lang.org/examples/radix-sort-key.html)
- [Merge sort](https://futhark-lang.org/examples/merge-sort.html)
- [Removing duplicates](https://futhark-lang.org/examples/removing-duplicates.html)
- [Abstract data types](https://futhark-lang.org/examples/abstract-data-types.html)
- [Complex numbers](https://futhark-lang.org/examples/complex-numbers.html)
- [Testing for associativity](https://futhark-lang.org/examples/testing-associativity.html)
- [Reducing or scanning without a neutral element](https://futhark-lang.org/examples/no-neutral-element.html)
- [Holes](https://futhark-lang.org/examples/holes.html)
- [Kahan summation](https://futhark-lang.org/examples/kahan.html)
- [Random numbers](https://futhark-lang.org/examples/random-numbers.html)
- [Gaussian blur \(with Python integration\)](https://futhark-lang.org/examples/gaussian-blur.html)
- [Three\-dimensional vectors](https://futhark-lang.org/examples/3d-vectors.html)
- [Faking nominal types](https://futhark-lang.org/examples/nominal-types.html)
- [Triangular matrices](https://futhark-lang.org/examples/triangular.html)
- [Binary search](https://futhark-lang.org/examples/binary-search.html)
- [AD with dual numbers](https://futhark-lang.org/examples/dual-numbers.html)
- [Variance](https://futhark-lang.org/examples/variance.html)
- [Matching parentheses](https://futhark-lang.org/examples/parens.html)
- [Evaluating polynomials](https://futhark-lang.org/examples/polynomials.html)
- [Line fitting](https://futhark-lang.org/examples/line-fitting.html)
- [Decoding MNIST data files](https://futhark-lang.org/examples/mnist.html)
## [Automatic differentiation](https://futhark-lang.org/examples.html#automatic-differentiation)
- [Forward\-mode automatic differentiation](https://futhark-lang.org/examples/forward-ad.html)
- [Reverse\-mode automatic differentiation](https://futhark-lang.org/examples/reverse-ad.html)
- [Newton’s Method](https://futhark-lang.org/examples/newton-ad.html)
## [Literate Futhark](https://futhark-lang.org/examples.html#literate-futhark)
- [Basic use of literate Futhark](https://futhark-lang.org/examples/literate-basics.html)
- [Generating videos with literate Futhark](https://futhark-lang.org/examples/literate-video.html)
- [Reading and writing files in literate Futhark](https://futhark-lang.org/examples/literate-files.html)
## [Plotting](https://futhark-lang.org/examples.html#plotting)
Literate Futhark allows direct use of[gnuplot](http://www.gnuplot.info/)\. These examples show simple and common cases\.
- [Plotting a histogram](https://futhark-lang.org/examples/plot-histogram.html)
## [Examples from Dex](https://futhark-lang.org/examples.html#examples-from-dex)
The following examples are ported from[Dex](https://github.com/google-research/dex-lang), a dependently typed functional array language that uses a somewhat different approach to describing loop processing\. We've tried to keep the original naming scheme and programming style\.
- [Prelude](https://futhark-lang.org/examples/dex-prelude.html)
- [Mandelbrot set](https://futhark-lang.org/examples/dex-mandelbrot.html)
- [Multi\-step ray tracer](https://futhark-lang.org/examples/dex-raytrace.html)
- [Monte Carlo estimates of pi](https://futhark-lang.org/examples/dex-pi.html)
- [Brownian motion](https://futhark-lang.org/examples/dex-brownian-motion.html)
- [Sierpinski triangle](https://futhark-lang.org/examples/dex-sierpinski.html)
## [External examples](https://futhark-lang.org/examples.html#external-examples)
- [OCaml\-GPU wordcount example](https://github.com/ArulselvanMadhavan/ocaml_gpu_wordcount)by calling Futhark\.
## [Projects using Futhark](https://futhark-lang.org/examples.html#projects-using-futhark)
The majority of written Futhark code is probably still Futhark’s own test and benchmark suites\. However, there are some programs that have been written in Futhark because it was a good tool for the job, and not just to test the compiler\. A possibly incomplete list:
[Diving Beet](https://github.com/Athas/diving-beet)is a*falling sand*game, which is a kind of simple particle simulator toy\. Its main purpose is to produce pretty effects\. There is a[blog post](https://futhark-lang.org/blog/2016-12-04-diving-beet.html)with details and a video\.
[Futball](https://github.com/Athas/futball)is a game about avoiding getting hit by balls\. The rendering engine is a ray tracer written in Futhark\.
[Futcam](https://github.com/nqpz/futcam)is an application that applies stacks of interactively configurable filters to a webcam stream\. Futhark is used to implement the filters\.
[Futracer](https://github.com/nqpz/futracer)is a fairly slow brute\-force ray tracer written in Futhark\.
[Futswirl](https://github.com/nqpz/futswirl)is a fractal generator based on[iterated function systems](https://en.wikipedia.org/wiki/Iterated_function_system)\.
[Neptune](https://github.com/filecoin-project/neptune)is an implementation of the[Poseidon hash function](https://www.poseidon-hash.info/)tuned for[Filecoin](https://filecoin.io/), where[the GPU parts](https://github.com/filecoin-project/neptune-triton)have been implemented in Futhark\.
[Palathark](https://githepia.hesge.ch/orestis.malaspin/palathark)is a Futhark implementation of the lattice Boltzmann method\.
[Ray Tracing in One Weekend in Futhark](https://github.com/athas/raytracinginoneweekendinfuthark)and[Ray Tracing: the Next Week in Futhark](https://github.com/athas/raytracingthenextweekinfuthark/)are implementations based on Peter Shirley's book series\. These are by no means real\-time ray tracers, but support advanced effects and make use of acceleration structures like BVH trees\.
[Pareas](https://github.com/Snektron/pareas)is a compiler implemented in Futhark and C\+\+\.
An introductory article on systems programming covering fundamentals such as bit manipulation, parsing, filesystems, syscalls, and memory management, targeted at programmers.
This article presents a minimalist 7-line interpreter for a Turing-equivalent functional language based on lambda calculus, demonstrating the eval/apply design pattern.
The article recommends Peter Naur's 'Programming as Theory Building,' arguing that programming is fundamentally about constructing and communicating a mental model of the software rather than just writing code.
An essay exploring seven foundational programming language archetypes (ur-languages) that underlie most modern programming languages, arguing that learning fundamentals rooted in these archetypes is more important than the specific language chosen.
The author shares their experience building a custom programming language for a game modding system, discussing design motivations and technical challenges. The post serves as a reflection on the feasibility and complexity of implementing a non-toy language for personal projects.