# Building Langton's Ant in Elm

DevFeed: [Building Langton's Ant in Elm](<https://devfeed.tech/articles/building-langton-s-ant-in-elm-52510.md>)

Original publisher: [Read original article](<http://www.jackfranklin.co.uk/blog/langtons-ant-elm-lang/>)

Published: 2017-03-14T00:00:00Z

Content type: tutorial

Language: en

Sources: [Jack Franklin](<https://devfeed.tech/sources/jack-franklin.md>)

Topics: [Elm](<https://devfeed.tech/topics/elm.md>), [Cellular automaton](<https://devfeed.tech/topics/cellular-automaton.md>), [data type](<https://devfeed.tech/topics/data-type.md>), [Algorithms](<https://devfeed.tech/topics/algorithms.md>), [Functional programming](<https://devfeed.tech/topics/functional-programming.md>), [union types](<https://devfeed.tech/topics/union-types.md>), [enum](<https://devfeed.tech/topics/enum.md>), [Learning](<https://devfeed.tech/topics/learning.md>), [Publish-subscribe pattern](<https://devfeed.tech/topics/pubsub.md>)

Tags: [board](<https://devfeed.tech/tags/board.md>), [building](<https://devfeed.tech/tags/building.md>), [conway](<https://devfeed.tech/tags/conway.md>), [css](<https://devfeed.tech/tags/css.md>), [elm](<https://devfeed.tech/tags/elm.md>), [game](<https://devfeed.tech/tags/game.md>), [type-alias](<https://devfeed.tech/tags/type-alias.md>), [types](<https://devfeed.tech/tags/types.md>), [union](<https://devfeed.tech/tags/union.md>)

## AI overview

The article implements Langton's Ant in Elm by modeling the ant, direction, cell colors, coordinates, and board with Elm types. It stores cells in a coordinate-keyed `Dict`, treating missing cells as white, then advances each millisecond tick by flipping the current cell, turning according to its original color, and moving one square. It uses `create-elm-app` to start the project and `elm-lang/svg` to render cells and the ant in a centered SVG viewBox. The author presents explicit types as a way to shape the application and let the compiler guide later changes. The supplied text ends with the implementation and rendering; it does not provide the linked full code.

## Source excerpt

Last week I attended the Elm London meetup, arranged by Kris Jenkins, who always produces a great selection of challenges for people to take on for all skill levels. Along with Isaac, we decided to take on Kris' challenge to build Langton's Ant in Elm. Langton's Ant Langton's Ant is a game similar to Conway's Game of Life in that cells on a game board change from being alive to dead based on their surroundings and certain criteria. In Langton's Ant there is a small ant on the board that moves and changes squares as it goes. The ant starts on any random square, and on each move it: Based on the colour of the square, it either turns 90 degrees right, or 90 degrees left. Changes the colour of the square it's on from black to white, or vice versa. Moves forward one square in the direction it's facing. The fact that the game has well defined rules makes it perfect for a short hack event, so we set about building it in Elm. Getting started I used create-elm-app to quickly get the app going. It's a really handy way to get a working application and local server up without having to think about it, and I highly recommend it. Defining types When working in Elm the first thing I always do is define the types for the key concepts of the application. Looking through Langton's ant, we could see we'd need to model: The concept of an Ant. Cells on a game Board. Coords which cells are positioned at on the board. The Direction of the ant. The Colour of a cell. Starting with the easier ones, a Direction is a union type that can be one of Up, Down, Left or Right: type Direction = Up | Down | Left | Right And similarly, a Colour is either White or Black: type Colour = White | Black And a coordinate, or Coord, is a tuple of (x, y): type alias Coord = ( Int, Int ) Once you have these, it's easy to combine them to make up the "bigger" types. A cell is a record that has a coordinate pair and a colour: type alias Cell = { position : Coord , colour : Colour } And the Ant is a record with a po