iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🪃

Basic Stack Manipulation Functions in Uiua

に公開

https://zenn.dev/hatappo/articles/9a7f4e601df235

This is also a continuation of the article above.

Uiua is a stack-based programming language[1], and it provides a full set of basic stack manipulation functions. Let's look at them in order.

· noop

A function that does nothing and returns the given value as is.

· [10 20 30 40 50]
# ↓
[10 20 30 40 50]

If you pass it as a callback function to ∵ each (something like forEach), it returns the target array as is. While the example above returns the array itself, the one below performs a noop on each element of the array, resulting in the same array being returned.

∵· [10 20 30 40 50]
# ↓
[10 20 30 40 50]

; pop

Removes the top element. Does the glyph represent the image of something popping off from the very top?

[; 10 20 30 40 50]
# ↓
[20 30 40 50]
[;; 10 20 30 40 50]
# ↓
[30 40 50]

. duplicate

Copies the top element of the stack and pushes it onto the top of the stack.

. [10 20 30 40 50] # Note that Uiua evaluates values from the right
# ↓
[10 20 30 40 50]
[10 20 30 40 50]

Using . to keep intermediate stack states is a common practice in explanatory writing. It is also often seen when manipulating arrays to create information corresponding to a copied array and then applying it back to the original array.

∶ flip

Swaps the positions of the top two elements.

[∶ 10 20 30 40 50]
# ↓
[20 10 30 40 50]
[∶ 10 20 30 40 50]
# ↓
[20 10 30 40 50]

, over

Copies the second value from the top and adds it to the top.

[, 10 20 30 40 50]
# ↓
[20 10 20 30 40 50]

→ dip

Temporarily sets aside the top value, applies a function to the rest of the stack in that state, and then returns the original top element back to the top.

In the example below, the leading 10 is set aside for a moment, the + (addition) operation is performed on the next values 20 and 30 to push the result 50 onto the stack, and then the set-aside 10 is returned to the top of the stack.

[→+ 10 20 30 40 50]
# ↓
[10 50 40 50]

Interestingly, by layering them consecutively, you can apply a function to any arbitrary position within the stack.

[+ 10 20 30 40 50]
[→+ 10 20 30 40 50]
[→→+ 10 20 30 40 50]
[→→→+ 10 20 30 40 50]
[→→→→+ 10 20 30 40 50] # Error: Stack was empty when evaluating argument 2

# ↓

[30 30 40 50]
[10 50 40 50]
[10 20 70 50]
[10 20 30 90]
Error: Stack was empty when evaluating argument 2

Note:
→ dip was committed just a few hours before writing this article (October 1, 2023, 17:13). If you find that dip is not available in your environment, please rebuild Uiua.

Also, with the introduction of dip, the ↷ roll and ↶ unroll functions have been deprecated. This is likely because the functionality of roll and unroll has been sufficiently generalized.

[+↷ 1 2 3]
[∶→+ 1 2 3]
[+↶ 1 2 3]
[∶→+∶ 1 2 3]

# ↓

[5 1]
[5 1]
[4 2]
[4 2]

~ trace

Displays the top element of the stack. However, unlike pop, it does not consume the value and it remains. In that sense, its behavior is somewhat similar to the common stack operation peek. It is primarily for debugging purposes.

main.ua
~ [10 20 30 40 50]
# ↓
main.ua:1:1 [10 20 30 40 50]
[10 20 30 40 50]

The continuation is at:

https://zenn.dev/hatappo/articles/a6dad4af411ecf

脚注
  1. However, it is not stack-oriented; rather, it adopts the stack as a convenient tool for holding variables. ↩︎

Discussion