# meta_lisp_mock **Repository Path**: SuperWindcloud/meta_lisp_mock ## Basic Information - **Project Name**: meta_lisp_mock - **Description**: CPP元编程模拟lisp - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-07-18 - **Last Updated**: 2024-07-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## MetaLisp 使用 C++ 模板元编程来模拟 Scheme 的 car、cdr、cons、lisp 等语法。需要 C++ 17 支持。 使用 clang 编译器测试。它默认的模板嵌套是 1024 层。也可以用编译选项 ### 模拟 Scheme 函数 [MetaLisp.hpp](./MetaLisp.hpp) 用于模拟 Scheme 的一些常用函数。比如下面 Scheme 函数 ```Scheme (define (append a b) (if (null? a) b (cons (car a) (append (cdr a) b)))) (define (reverse items) (define (iter items result) (if (null? items) result (iter (cdr items) (cons (car items) result)))) (iter items null)) ``` C++ 相应写成 ```C++ template struct append : public if_else, b, cons, append, b>>> {}; template struct reverse { template struct iter : public if_else, result, iter, cons, result>>> {}; using type = typename iter::type; using tag = typename type::tag; }; ``` C++ 每个模板类,相当于 Scheme 的一个函数。结果存放在模板类的 type 中。 ### 元编程例子 * [求解八皇后问题](./examples/queen.hpp) * [计算阶乘](./examples/factorial.hpp) * [平方根](./examples/sqrt.hpp) * [huffman 编码](./examples/huffman.hpp)