by oumua_don17 on 11/15/24, 7:47 PM with 71 comments
by pjmlp on 11/15/24, 9:22 PM
#include <iostream>
consteval long factorial (int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
std::cout << factorial(7) << std::endl;
}
Exercise for the reader if using VC++ or clang/ninja, use import std instead.-- https://godbolt.org/z/TWe11hM6j
Nicely put 5040 in ESI register at compile time.
Granted, C++ isn't Lisp, but already has quite a room for creativity at compile time, and C++26 might finally have compile time reflection as well.
by thunkingdeep on 11/15/24, 10:55 PM
Not to dog on C++ unfairly, CTE is pretty neat after all.
Funnily enough, PGs “On Lisp” has some really neat macros in it that demonstrate capabilities that just can’t be replicated with template based macros, iirc.
by lispm on 11/15/24, 10:21 PM
(defmacro factorial (n)
(labels ((fact (m)
(if (= m 0)
1
(* m (fact (1- m))))))
`,(fact n)))
The `, has no use here and can be removed. Here the backquote and the evaluation just returns the computed value.Thus, this is okay:
(defmacro factorial (n)
(labels ((fact (m)
(if (= m 0)
1
(* m (fact (1- m))))))
(fact n)))
LABELS defines local recursive functions. The macro returns the result of calling FACT, which is a number and which is a valid form in Common Lisp. A number evaluates to itself. CL-USER > (macroexpand-1 '(factorial 10))
3628800
T
by scott_s on 11/15/24, 8:51 PM
(My parenthetical "maybe" is that I don't think compilers have to compute constexpr expressions at compile time. The compiler will be forced to when such expressions are used in contexts that require values at compile time. But I think it would be permissible for a compile to defer computation of a constexpr to runtime if the value isn't needed until runtime.)
by liontwist on 11/15/24, 8:37 PM
C++ macros can only take types and numbers (until variadic), and writing any code to operate on those inputs is challenging.
by unnah on 11/16/24, 6:06 PM
by rottc0dd on 11/16/24, 4:39 AM
HN discussion: https://news.ycombinator.com/item?id=31199992
by binary132 on 11/16/24, 5:09 AM
by James_K on 11/15/24, 10:52 PM
by mgaunard on 11/15/24, 11:45 PM
by forrestthewoods on 11/15/24, 9:42 PM
https://www.forrestthewoods.com/blog/using-jais-unique-and-p...
by mwkaufma on 11/16/24, 2:31 AM
by anothername12 on 11/16/24, 7:50 AM