Functions in Haskell

* haskell programming
[2025-12-28 Sun]

1. Defining Functions

Functions are defined simply and used simply. The function name is followed by parameters separated by spaces. But when defining functions, there’s a = and after that we define what the function does.(https://learnyouahaskell.github.io/starting-out.html#babys-first-functions)

doubleMe :: Int -> Int
doubleMe x = x + x
doubleMe 2

But what are the Int -> things? Those are type declarators, and the :: is a type classifier. doubleMe takes an int and returns an Int. It would fail if we did something like

doubleMe :: Int -> Int
doubleMe x = x + x
doubleMe 9 9  

or

doubleMe :: Int -> Int
doubleMe x = x + x
doubleMe 3.1

1.1. Example 1

Lets take a more complicated example. This is the decision-making system for Ethics(5). We start by doing a pattern match on the stimulus field inside the ctx (Input Context)

decide :: Context -> Action 
decide ctx  =
  case stimulus ctx of
    Ambiguous ->
      if pressure ctx > 4
      then WithdrawalExternal
      else MinimalResponse
    BoundaryViolation _ ->
      WithdrawExternal
    AidRequest _ ->
      if aidAllowed ctx
      then ProvideAid
      else WithdrawInternal
    Neutral ->
      if pressure ctx > 7
      then WithdrawInternal
      else if minimalPositiveAllowed ctx
           then MinimalPositive
           else NoOp

If the stimulus is ambiguous and the pressure is above 4, then you will withdraw externally, else you will give a minimalresponse. AidRequest _ ->: AidRequest usually requires an agent, but here it doesnt matter which agent did it hence the wildcard _, we will always withdraw on boundary violation. Then on neutral stimuli we will check if minimalpositive is allowed.

1.2. Just, and Maybe

Maybe is the "wrapper" (the type). It is defined like this:

data Maybe a = Nothing | Just a

  • a: This is a type variable (like Int, String, or your Agent type).
  • Nothing: Represents a failed search, an empty field, or "no value."
  • Just a: Represents success, containing the actual value.

2. Elsewhere

2.1. References

2.2. In my garden

Notes that link to this note (AKA backlinks).

Recent changes. Attachment Index Tag Index Bibliography Index Source.