This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,3 @@
Some programming languages allow calculation of values at compile time. For this task, calculate 10! at compile time. Print the result when the program is run.
Discuss what limitations apply to compile-time calculations in your language.

View file

@ -0,0 +1,7 @@
with Ada.Text_Io;
procedure CompileTimeCalculation is
Factorial : constant Integer := 10*9*8*7*6*5*4*3*2*1;
begin
Ada.Text_Io.Put(Integer'Image(Factorial));
end CompileTimeCalculation;

View file

@ -0,0 +1,16 @@
with Ada.Text_Io;
procedure CompileTimeCalculation is
function Factorial (Int : in Integer) return Integer is
begin
if Int > 1 then
return Int * Factorial(Int-1);
else
return 1;
end if;
end;
Fact10 : Integer := Factorial(10);
begin
Ada.Text_Io.Put(Integer'Image(Fact10));
end CompileTimeCalculation;

View file

@ -0,0 +1,12 @@
with Ada.Text_IO;
procedure Unbounded_Compile_Time_Calculation is
F_10 : constant Integer := 10*9*8*7*6*5*4*3*2*1;
A_11_15 : constant Integer := 15*14*13*12*11;
A_16_20 : constant Integer := 20*19*18*17*16;
begin
Ada.Text_IO.Put_Line -- prints out
("20 choose 10 =" & Integer'Image((A_11_15 * A_16_20 * F_10) / (F_10 * F_10)));
-- Ada.Text_IO.Put_Line -- would not compile
-- ("Factorial(20) =" & Integer'Image(A_11_15 * A_16_20 * F_10));
end Unbounded_Compile_Time_Calculation;

View file

@ -0,0 +1,2 @@
Ada.Text_IO.Put_Line -- would not compile
("Factorial(20) =" & Integer'Image(A_11_15 * A_16_20 * F_10));

View file

@ -0,0 +1 @@
CONST factorial10 = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10

View file

@ -0,0 +1,2 @@
DIM factorial10 AS LONG
factorial10 = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10

View file

@ -0,0 +1,18 @@
#include <iostream>
template<int i> struct Fac
{
static const int result = i * Fac<i-1>::result;
};
template<> struct Fac<1>
{
static const int result = 1;
};
int main()
{
std::cout << "10! = " << Fac<10>::result << "\n";
return 0;
}

View file

@ -0,0 +1,12 @@
#include <stdio.h>
constexpr int factorial(int n) {
return n ? (n * factorial(n - 1)) : 1;
}
constexpr int f10 = factorial(10);
int main() {
printf("%d\n", f10);
return 0;
}

View file

@ -0,0 +1,12 @@
_main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
call ___main
movl $3628800, 4(%esp)
movl $LC0, (%esp)
call _printf
movl $0, %eax
leave
ret

View file

@ -0,0 +1,10 @@
#include <stdio.h>
#include <order/interpreter.h>
#define ORDER_PP_DEF_8fac ORDER_PP_FN( \
8fn(8X, 8seq_fold(8times, 1, 8seq_iota(1, 8inc(8X)))) )
int main(void) {
printf("10! = %d\n", ORDER_PP( 8to_lit( 8fac(10) ) ) );
return 0;
}

View file

@ -0,0 +1,2 @@
(defn fac [n] (apply * (range 1 (inc n))))
(defmacro ct-factorial [n] (fac n))

View file

@ -0,0 +1,2 @@
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun factorial ...))

View file

@ -0,0 +1,6 @@
(defmacro ct-factorial (n)
(factorial n))
...
(print (ct-factorial 10))

View file

@ -0,0 +1,6 @@
(defmacro ct-factorial (n)
`(quote ,(factorial n)))
; or, equivalently,
(defmacro ct-factorial (n)
`',(factorial n))

View file

@ -0,0 +1 @@
(print (load-time-value (factorial 10)))

View file

@ -0,0 +1 @@
(print (#. (factorial 10)))

View file

@ -0,0 +1,4 @@
(define-compiler-macro factorial (&whole form arg)
(if (constantp arg)
(factorial arg)
form))

View file

@ -0,0 +1,15 @@
import core.stdc.stdio;
long fact(long x) {
long result = 1;
foreach (i; 2 .. x + 1)
result *= i;
return result;
}
void main() {
// enum means "compile-time constant", it forces CTFE.
enum fact10 = fact(10);
printf("%ld\n", fact10);
}

View file

@ -0,0 +1,11 @@
__Dmain
push EAX
mov EAX,offset FLAT:_DATA
push 0
push 0375F00h
push EAX
call near ptr _printf
add ESP,0Ch
xor EAX,EAX
pop ECX
ret

View file

@ -0,0 +1 @@
const fact10 = Factorial(10);

View file

@ -0,0 +1,7 @@
: fac ( n -- n! ) 1 swap 1+ 2 max 2 ?do i * loop ;
: main ." 10! = " [ 10 fac ] literal . ;
see main
: main
.\" 10! = " 3628800 . ; ok

View file

@ -0,0 +1,9 @@
: more ( "digits" -- ) \ store "1234" as 1 c, 2 c, 3 c, 4 c,
parse-word bounds ?do
i c@ [char] 0 - c,
loop ;
create bignum
more 73167176531330624919225119674426574742355349194934
more 96983520312774506326239578318016984801869478851843
...

View file

@ -0,0 +1,8 @@
program test
implicit none
integer,parameter :: t = 10*9*8*7*6*5*4*3*2 !computed at compile time
write(*,*) t !write the value the console.
end program test

View file

@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Println(2*3*4*5*6*7*8*9*10)
}

View file

@ -0,0 +1,5 @@
{-# LANGUAGE QuasiQuotes, TemplateHaskell #-}
fact n = product [1..n]
main = print $([|fact 10|])

View file

@ -0,0 +1,6 @@
my $tenfactorial;
print "$tenfactorial\n";
BEGIN
{$tenfactorial = 1;
$tenfactorial *= $_ foreach 1 .. 10;}

View file

@ -0,0 +1 @@
my $tenfactorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2;

View file

@ -0,0 +1,5 @@
(de fact (N)
(apply * (range 1 N)) )
(de foo ()
(prinl "The value of fact(10) is " `(fact 10)) )

View file

@ -0,0 +1,3 @@
/*REXX program to compute 10!*/ say '10! =' !(10); exit
!: procedure; !=1; do j=2 to arg(1); !=!*j; end; return !

View file

@ -0,0 +1,21 @@
#lang racket
;; Import the math library for compile-time
;; Note: included in Racket v5.3.2
(require (for-syntax math))
;; In versions older than v5.3.2, just define the function
;; for compile-time
;;
;; (begin-for-syntax
;; (define (factorial n)
;; (if (zero? n)
;; 1
;; (factorial (- n 1)))))
;; define a macro that calls factorial at compile-time
(define-syntax (fact10 stx)
#`#,(factorial 10))
;; use the macro defined above
(fact10)

View file

@ -0,0 +1,8 @@
proc makeFacExpr n {
set exp 1
for {set i 2} {$i <= $n} {incr i} {
append exp " * $i"
}
return "expr \{$exp\}"
}
eval [makeFacExpr 10]

View file

@ -0,0 +1,9 @@
% tcl::unsupported::disassemble script [makeFacExpr 10]
ByteCode 0x0x4de10, refCt 1, epoch 3, interp 0x0x31c10 (epoch 3)
Source "expr {1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10}"
Cmds 1, src 45, inst 3, litObjs 1, aux 0, stkDepth 1, code/src 0.00
Commands 1:
1: pc 0-1, src 0-44
Command 1: "expr {1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10}"
(0) push1 0 # "3628800"
(2) done