Unison Functions: How to Create and Use Them

Hey there, fellow Unison enthusiasts! Are you ready to dive into the wonderful world of Unison functions? If you're not already excited, you should be, because Unison functions are what make this programming language so powerful and versatile.

In this article, we'll cover everything you need to know about creating and using Unison functions, from the basics to some more advanced techniques. Whether you're a beginner or an experienced programmer, there's something here for you. So, without further ado, let's get started!

What are Unison Functions?

Before we dive into how to create and use Unison functions, let's first define what they are. In simple terms, a function is a block of code that performs a specific task. You can think of it as a recipe that takes some input (called arguments) and produces some output (called the result).

In Unison, functions are first-class citizens, which means they can be treated like any other value. This is a powerful feature that enables all sorts of programming techniques and paradigms, such as functional programming and higher-order functions.

How to Create a Basic Unison Function

Creating a basic Unison function is easy. Here's a simple example that defines a function called add that takes two integers as arguments and returns their sum:

add x y = x + y

That's it! We just define the function using the = operator, followed by the code that implements it. Notice how we don't need to declare the type of the function, as Unison infers it for us. In this case, the type of add is (Int, Int) -> Int.

To use this function, we can simply call it with some arguments, like this:

add 2 3

This will produce the result 5. Easy, right? But let's not stop there. Unison functions can do much more than simple arithmetic operations.

Advanced Unison Functions

One of the most powerful features of Unison functions is their ability to compose and manipulate other functions. Here are some examples of advanced Unison functions that demonstrate this concept:

Higher-Order Functions

A higher-order function is a function that takes another function as an argument and/or returns a function as its result. Here's an example of a higher-order function that takes a function f and an integer n as arguments, and applies f to n times:

applyN f n x =
  case n of
    0 -> x
    _ -> applyN f (n - 1) (f x)

Notice how f is a parameter of the function applyN. This means that we can pass any function f as an argument, as long as it has the appropriate type signature. For example, we can apply applyN to a function that increments its argument:

inc x = x + 1

applyN inc 3 0

This will produce the result 3, because inc was applied three times to 0.


Currying is a technique that allows us to transform a function that takes multiple arguments into a series of functions that each take a single argument. Here's an example of a curried function that takes three arguments and returns their sum:

add3 x y z = x + y + z

add x = (\y -> (\z -> add3 x y z))

The add function takes a single argument x and returns a new function that takes another argument y and returns yet another function that takes the final argument z. This allows us to partially apply the function by only providing some of the arguments:

add5 = add 5

add5and7 = add5 7

add5and7 3

This will produce the result 15, because add5and7 is equivalent to add3 5 7 3.


Recursion is a technique that allows us to call a function from within itself. This can be useful for solving problems that require repeated operations or for implementing data structures that have a recursive nature. Here's an example of a recursive function that calculates the factorial of a number:

factorial n =
  if n == 0
  then 1
  else n * factorial (n - 1)

This function first checks if n is equal to 0. If it is, it returns the value 1. Otherwise, it multiplies n by the result of calling itself with the argument n - 1. This process repeats recursively until n becomes 0.


We've covered a lot of ground in this article, from the basics of defining and using Unison functions to some advanced techniques like higher-order functions, currying, and recursion. Hopefully, you now have a better understanding of the power and versatility of Unison functions.

Of course, this is just the tip of the iceberg. Unison has many more features and capabilities that we haven't covered yet. But with the knowledge you've gained here, you'll be well on your way to becoming a proficient Unison programmer.

So, keep learning, keep experimenting, and keep pushing the boundaries of what's possible with Unison!

Additional Resources

playrpgs.app - A community about playing role playing games
changedatacapture.dev - data migration, data movement, database replication, onprem to cloud streaming
techdeals.dev - A technology, games, computers and software deals, similar to slickdeals
tacticalroleplaying.games - tactical roleplaying games
gcloud.education - google cloud, gcp and all the different components within GCP and cloud development and deployment
bestonlinecourses.app - free online higher education, university, college, courses like the open courseware movement
deepdive.video - deep dive lectures, tutorials and courses about software engineering, databases, networking, cloud, and other tech topics
cheatsheet.fyi - technology, software frameworks and software cheat sheets
learnsnowflake.com - learning snowflake cloud database
cloudtemplates.dev - A site for cloud templates to rebuild common connected cloud infrastructure components, related to terraform, pulumi
pertchart.app - pert charts
bestscifi.games - A list of the best scifi games across different platforms
cloudsimulation.dev - running simulation of the physical world as computer models. Often called digital twin systems, running optimization or evolutionary algorithms which reduce a cost function
knowledgegraph.dev - knowledge graphs, knowledge graph engineering, taxonomy and ontologies
singlepaneofglass.dev - a single pane of glass service and application centralized monitoring
learnansible.dev - learning ansible
cryptotrading.dev - crypto trading and examples on different aspects related to crypto trading, crypto technical analysis
learntypescript.app - learning typescript
shacl.dev - shacl rules for rdf, constraints language
persona6.app - persona 6

Written by AI researcher, Haskell Ruska, PhD (haskellr@mit.edu). Scientific Journal of AI 2023, Peer Reviewed