I encountered a situation that need to break a loop while invoking Scheme code in LilyPond. Typically LilyPond offers the `for-each` function which simulates the for-loop in most programming languages. However, the `break` functionality isn't explicitly offered. There seems to be a function named `break` in Scheme but it is not what `break` in most common programming languages mean. I need to find a proper way to implement the `break`.

The purpose of my code is to get a value or an entry handle from a multi-layer `hash-table` in Scheme. Here is a simulation of the process in Python:

```py

defdeepGet( dic, keys, default= None):
target=dic
forkeyinkeys:
ifkeyintarget: target=target[ key]
else: returndefault
returntarget

```

However, in Scheme, returning a value inside a loop is troublesome. I must first break the loop and then return the value. So what is the correct way of doing this?

My current code is as follows:

```ly

#(define* ((_hash-table-deep-ref getter) h keys #:optional (fallback #f))
  (let* ((target h))
    (for-each
      (lambda (key)
        (set! target (getter target key)))
      keys)
    target))

#(define hash-deep-ref (_hash-table-deep-ref hash-ref))
#(define hashq-deep-ref (_hash-table-deep-ref hashq-ref))
#(define hashv-deep-ref (_hash-table-deep-ref hashv-ref))

```

The code works fine only if all the sub-tables are present for the given keys. If any of the sub-table is not present for one key in the middle of the key list, this function would fail. My ideal case is that when such a case happens, the fallback value should be returned. Also I want to create a version of the function getting the hash entry handle from a multi-layer hash table.

Reply via email to