Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
d0cf215b
Commit
d0cf215b
authored
Jul 12, 2008
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add new test - factorial by inc and dec
SVN=126937
parent
3856e45e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
131 additions
and
0 deletions
+131
-0
peano.go
test/peano.go
+131
-0
No files found.
test/peano.go
0 → 100644
View file @
d0cf215b
// $G $F.go && $L $F.$A && ./$A.out
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
main
type
Number
struct
{
next
*
Number
}
// -------------------------------------
// Peano primitives
func
zero
()
*
Number
{
return
nil
;
}
func
is_zero
(
x
*
Number
)
bool
{
return
x
==
nil
;
}
func
add1
(
x
*
Number
)
*
Number
{
e
:=
new
(
Number
);
e
.
next
=
x
;
return
e
;
}
func
sub1
(
x
*
Number
)
*
Number
{
return
x
.
next
;
}
func
add
(
x
,
y
*
Number
)
*
Number
{
if
is_zero
(
y
)
{
return
x
;
}
return
add
(
add1
(
x
),
sub1
(
y
));
}
func
mul
(
x
,
y
*
Number
)
*
Number
{
if
is_zero
(
x
)
||
is_zero
(
y
){
return
zero
();
}
return
add
(
mul
(
x
,
sub1
(
y
)),
x
);
}
func
fact
(
n
*
Number
)
*
Number
{
if
is_zero
(
n
)
{
return
add1
(
zero
());
}
return
mul
(
fact
(
sub1
(
n
)),
n
);
}
// -------------------------------------
// Helpers to generate/count Peano integers
func
gen
(
n
int
)
*
Number
{
if
n
>
0
{
return
add1
(
gen
(
n
-
1
));
}
return
zero
();
}
func
count
(
x
*
Number
)
int
{
if
is_zero
(
x
)
{
return
0
;
}
return
count
(
sub1
(
x
))
+
1
;
}
func
check
(
x
*
Number
,
expected
int
)
{
var
c
=
count
(
x
);
if
c
!=
expected
{
panic
"error: found "
,
c
,
"; expected "
,
expected
,
"
\n
"
;
}
}
// -------------------------------------
// Test basic functionality
func
verify
()
{
check
(
zero
(),
0
);
check
(
add1
(
zero
()),
1
);
check
(
gen
(
10
),
10
);
check
(
add
(
gen
(
3
),
zero
()),
3
);
check
(
add
(
zero
(),
gen
(
4
)),
4
);
check
(
add
(
gen
(
3
),
gen
(
4
)),
7
);
check
(
mul
(
zero
(),
zero
()),
0
);
check
(
mul
(
gen
(
3
),
zero
()),
0
);
check
(
mul
(
zero
(),
gen
(
4
)),
0
);
check
(
mul
(
gen
(
3
),
add1
(
zero
())),
3
);
check
(
mul
(
add1
(
zero
()),
gen
(
4
)),
4
);
check
(
mul
(
gen
(
3
),
gen
(
4
)),
12
);
check
(
fact
(
zero
()),
1
);
check
(
fact
(
add1
(
zero
())),
1
);
check
(
fact
(
gen
(
5
)),
120
);
}
// -------------------------------------
// Factorial
func
main
()
{
verify
();
for
i
:=
0
;
i
<=
10
;
i
++
{
print
i
,
"! = "
,
count
(
fact
(
gen
(
i
))),
"
\n
"
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment