You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何正确解读使用foldl的SML函数?arr参数传递逻辑存疑

Understanding How arr is Passed in Your SML print_mat Function

Hey there! Let's break down this code piece by piece so you can see exactly how arr gets picked up and used in the closure. I know SML's syntax can feel a bit verbose at first, so simplifying it will help make things clearer.

First, Let's Recap the Core Code

Your original function:

fun print_mat mat = 
    let val _ = (Array.foldl ( fn (arr, _) => 
        let val _ = (print_arr arr) in () end ) () mat) 
    in () end;

And your example usage:

val mat = Array.fromList[ (Array.fromList [0, 1, 1, 0, 1]), (Array.fromList [1, 0, 1, 0, 0]) ]
val _ = print_mat mat

Let's Simplify the Code First

A lot of the let val _ = ... in () end boilerplate is unnecessary here because we're only using side effects (printing) instead of returning a meaningful value. We can rewrite print_mat to be much cleaner:

fun print_mat mat =
    Array.foldl (fn (arr, _) => print_arr arr) () mat

This does exactly the same thing as your original code—just easier to read.

Now, How Does arr Get Selected?

Let's focus on Array.foldl, which is the key here:

  • Array.foldl is a function that iterates over every element in an array. For each element, it passes two things to the anonymous function you give it:
    1. The current element of the array (this is your arr!)
    2. A "accumulator" value (we use _ here because we don't care about it—we're just using foldl to loop, not to build up a result)
  • The second argument to Array.foldl is () (the unit value), which is the initial value of the accumulator. Since we don't use the accumulator, this doesn't affect our code.
  • The third argument is mat—the 2D array you pass to print_mat.

Step-by-Step with Your Example

When you call print_mat mat:

  1. Array.foldl looks at the first element of mat: that's Array.fromList [0, 1, 1, 0, 1]. It passes this element as arr to the anonymous function.
  2. The anonymous function calls print_arr arr, which prints this 1D array.
  3. Next, Array.foldl moves to the second element of mat: Array.fromList [1, 0, 1, 0, 0]. Again, this becomes arr in the anonymous function, and print_arr prints it.
  4. Once all elements are processed, foldl returns the final accumulator value (()), which we ignore since we only care about the printing side effect.

Why Does the Closure Have Access to arr?

The anonymous function fn (arr, _) => print_arr arr is a closure that captures the arr parameter passed to it by Array.foldl. Every time foldl processes a new element in mat, it creates a new binding for arr pointing to that element, and the closure uses that binding to call print_arr.

In short: arr is just each individual 1D array inside your 2D mat array, and Array.foldl handles looping through them and passing each one to your closure.

内容的提问来源于stack exchange,提问作者nico

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:21:30