← 都来码 / Dolaima← 例程索引r/school/MANIFEST.md 原文

r/school/MANIFEST.md · 6540 字节 · 下载原文

# R 外部老程序语料(10 个)

抓取日期 / Fetched: 2026-10-14 · 语言 / Lang: `r`(`--lang r`,扩展名 `.r`)
编译判据 / Criterion: **只看编不编得过**
`bash scripts/vml-compat-external.sh r` → **通过 5 / 失败 5 / 共 10**(2026-10-15 复测;3 → 5 见下文 A1)

来源全部是 **Rosetta Code**(内容按 **GNU FDL 1.3** 发布)。每条的 URL 见下表。
挑的取向:R 的经典练习(排序 / 素数 / 递归 / 字符串 / 小游戏),**优先循环 + 标量的老式写法**,
避开绘图(口径②)与 `library(...)`。

## 逐条记录

| 文件名 | 来源 URL | 许可 | 行数 | 编译 | 第一条错误(失败时贴原文) |
|---|---|---|---|---|---|
| `towers_of_hanoi.r` | https://rosettacode.org/wiki/Towers_of_Hanoi | GNU FDL 1.3 | 16 | ✅ | — |
| `ackermann.r` | https://rosettacode.org/wiki/Ackermann_function | GNU FDL 1.3 | 18 | ✅ | — |
| `insertion_sort.r` | https://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort | GNU FDL 1.3 | 21 | ✅ | — |
| `prime_decomposition.r` | https://rosettacode.org/wiki/Prime_decomposition | GNU FDL 1.3 | 20 | ✅ | —(2026-10-15 起编得过,见 A1) |
| `binary_search.r` | https://rosettacode.org/wiki/Binary_search | GNU FDL 1.3 | 24 | ✅ | —(同上,见 A1) |
| `palindrome_detection.r` | https://rosettacode.org/wiki/Palindrome_detection | GNU FDL 1.3 | 14 | ❌ | `<input>:7: error: 未定义的函数 'func_nchar'(引用 2 次)` |
| `number_reversal_game.r` | https://rosettacode.org/wiki/Number_reversal_game | GNU FDL 1.3 | 29 | ❌ | `<input>:5: error: 未定义的函数 'func_sample'(引用 2 次)` |
| `bulls_and_cows.r` | https://rosettacode.org/wiki/Bulls_and_cows | GNU FDL 1.3 | 37 | ❌ | `…/bulls_and_cows.r:13:48: error: 意外的 token: LBracket([)` |
| `rep_string.r` | https://rosettacode.org/wiki/Rep-string | GNU FDL 1.3 | 36 | ❌ | `…/rep_string.r:36:5: error: 意外的 token: Gt(>)` |
| `gcd_euclid.r` | https://rosettacode.org/wiki/Greatest_common_divisor | GNU FDL 1.3 | 14 | ❌ | `未知的二元运算符: %gcd%` |

类别铺开:递归/控制流 `towers_of_hanoi` · `ackermann`;数值/算术 `prime_decomposition` · `gcd_euclid`;
数组/集合 `insertion_sort` · `binary_search`;字符串/文本 `palindrome_detection` · `rep_string`;
小游戏/模拟 `bulls_and_cows` · `number_reversal_game`。

**抄录说明**:多数是**整段照抄**。少数(`ackermann` / `binary_search` / `palindrome_detection` / `gcd_euclid`)
原页只给了函数定义,另有一段调用示例 —— 两份都取自**同一个页面**,拼成一个自包含文件;
文件头的注释里逐条写明了是哪一段。

## 三条口径(`scripts/compat-external-lint.py --dir …` 机械判)

10 个文件**全部 ✅ 无违规**(无汇编 / 无硬件地址 / 无界面库)。

## 这批反映出的编译器缺口(按原因归类)

### A1. ⚠ **`Lib/r/mathd.vml` 从来没进过链接集**(本条 2026-10-15 修掉,两个文件因此转 ✅)

前端把**认不得的函数名一律发射成 `CALL func_<R名字>`**(`func_*` 是这组**裸名别名**)。
旧判断写着"链接集里只有 C 风格的名字(`func_strlen` / `func_int_sqrt` …)、**没有 R 风格的名字**"
—— **那半句是错的**:`func_sqrt` / `func_floor` / `func_exp` / `func_log` …
**一直就在 `Lib/r/mathd.vml` 里**(`grep '^LABEL func_' Lib/r/mathd.vml` 有 41 个),
只是那份模块**不在** `Lib/r/builtin.vml` 的 `.linked` 清单里 ⇒ 链接期「未定义的函数」。
(`vmltool.config.xml` 的 `Libs=` 虽然写了 `mathd.vml`,但裸模块名按 `Lib/shared/` **优先**解析,
拿到的是**没有 `func_*` 的**共享实现体 —— 这就是"清单里明明有它"的错觉来源。)

修法:`tools/GenLib/Program.cs` 的聚合清单补 `"mathd"` ⇒ 数学这一族立刻能编。
**实测(2026-10-15,`--lang r` 逐个探)**:

| 现在能编过 | `sqrt` `floor` `exp` `log` `abs`(+ 连同原有的 `length` `sum` `mean` `sd` `min` `max` `sprintf` `cat` `print` `seq` `c` `list` `matrix`) |
|---|---|
| 仍编不过 | `ceiling`(**包装器叫 `func_ceil`**,名字对不上)· `round` · `nchar` · `paste` · `substr` · `toupper` · `rev` · `sort` · `rep` · `sample` · `strsplit` …(见 A2) |

⚠ **口径**:这一条只修了**链接**,没修**功能** —— R 前端把数值按 32 位传参,而
`mathd` 的包装器按 `.c` 里 `double` 的签名(8 字节位模式)收参数 ⇒ 实测 `floor(1.2)` 与
`floor(9.9)` 都返回 `0`、`cos(0)` 与 `cos(1)` 都返回 `1`。**"5/10"是编译口径**,
不等于这两个程序跑对了。

### A2. R 内置函数**根本没有实现/映射**(5 个失败里占 2 个:`nchar` / `sample`)

实测(逐个探针):

| 能编过 | `length` `sum` `mean` `sd` `min` `max` `abs` `sprintf` `cat` `print` `seq` `c` `list` `matrix` |
|---|---|
| **编不过** | `sqrt` `floor` `ceiling` `round` `exp` `log` `paste` `paste0` `nchar` `substr` `strsplit` `toupper` `tolower` `rev` `sort` `order` `which` `rep` `sample` `runif` `readline` `is.na` `all` `any` `ifelse` `apply` `sapply` `lapply` `as.integer` `as.character` `as.numeric` `stop` `data.frame` `cbind` `names` `table` `unlist` `factor` `replicate` `seq_along` `library` `plot` `integer()` |

⚠ 缺的**不是实现**而是**映射**:`func_sqrt` 没有,但 `func_int_sqrt`/`func_isqrt` 在;`nchar` 没有,
但 `func_strlen`/`func_str_len` 在;`toupper` 没有,但 `func_str_toupper` 在 —— 名字对不上而已。

### B. R 的语法形态解析不了(5 个失败里占 3 个:`LBracket` / `|>` / `%gcd%`)

- **用户自定义中缀运算符** `%gcd%`:`未知的二元运算符: %gcd%`(R 的招牌写法之一)。
- **双中括号下标** `strsplit(input, "")[[1]]`:`意外的 token: LBracket([)`。
- **原生管道** `|>`:`意外的 token: Gt(>)`(R 4.1 起的主要写法)。

### C. 另一条(探针量到的,不在这 10 个里)

**不加大括号的 `for` 体编不过**:`for(i in 1:n) s <- s + a[i]` → `error: R 解析错误: expected '{'(得到 Identifier)`;
而**不加大括号的 `if` 体可以**(`if (x > 1) y <- 1` ✅)。⇒ 写老式 R 时要给每个 `for` 补上 `{ }`。

## 复现

```bash
cd /Users/shitanyu/Desktop/source/mycoder/my-coder
python3 scripts/compat-external-lint.py --dir third_party/vml/compat-corpus/external/r
bash scripts/vml-compat-external.sh r
```