Hi Marek,

It looks like you've got a pretty good understanding :-)

The `filter` function will return a list, and the following `match` has three clauses that correspond to the three possible returns from `filter` that you listed.

1. The (sink) pattern matches a list with exactly one element; that element gets bound to an identifier called `sink`.

2. The () pattern matches the empty list

3. The x pattern matches anything else, and binds it to the identifier `x`.

These patterns are matched in the order they appear, so a single-element list will be handled from the first clause even though it would also match the last clause.

I hope this helps!

Ray.

On 16/01/2026 10:50, Marek Paśnikowski wrote:
Hello everyone.

My goal is to see how exactly service types and configurations are evaluated.
I am struggling to understand what is the primary control flow in the 
fold-services function.
I could just not bother working it out and look deeper into the function body.
However, the original intent is to learn — and this is an opportunity to learn 
more.

(define* (fold-services services #:key (target-type system-service-type))
  ...
  ...
  ...
  (match (filter ... services)
   ((sink) (run-with-state ...))
   (() (raise ...))
   (x (raise ...))))

My current understanding is that the filter may return one of three patterns:
1. An empty list when the list of services does not contain the target-type.
2. A single-element list when a correctly formed list of services is provided.
3. A many-element list when the list of services contains multiple services of 
target-type.

This does not make sense to me when comparing with the implementation of 
pattern matching in the function.
The Scheme Documentation on pattern matching is unhelpful, and neither are the 
cryptic binding names in the function.

I am confident that the filter function does return a list in every 
circumstance.
How do the matching patterns correlate to this filtered list?
The fold-services function is located in gnu/services.scm file.


Reply via email to