FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lambda/example_parser_test.go at master · KarpelesLab/lambda · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
KarpelesLab
/
lambda
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lambda
/
example_parser_test.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
53 lines (45 loc) · 1.16 KB
Breadcrumbs
lambda
/
example_parser_test.go
Copy path
File metadata and controls
53 lines (45 loc) · 1.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package
lambda
import
(
"fmt"
)
// Example demonstrating how to parse lambda expressions
func
ExampleParse
() {
// Parse an identity function
identity
,
_
:=
Parse
(
"λx.x"
)
fmt
.
Println
(
identity
)
// Output: λx.x
}
// Example parsing and reducing an application
func
ExampleParse_application
() {
// Parse and reduce (λx.x) y → y
expr
,
_
:=
Parse
(
"(λx.x) y"
)
reduced
,
_
:=
expr
.
BetaReduce
()
fmt
.
Println
(
reduced
)
// Output: y
}
// Example parsing Church numerals
func
ExampleParse_churchNumeral
() {
// Parse Church numeral 2
two
,
_
:=
Parse
(
"λf.λx.f (f x)"
)
fmt
.
Println
(
"Parsed:"
,
two
)
fmt
.
Println
(
"As integer:"
,
ToInt
(
two
))
// Output:
// Parsed: λf.λx.f (f x)
// As integer: 2
}
// Example parsing and evaluating arithmetic
func
ExampleParse_arithmetic
() {
// Parse PLUS 1 2
expr
,
_
:=
Parse
(
"(λm.λn.λf.λx.m f (n f x)) (λf.λx.f x) (λf.λx.f (f x))"
)
// Reduce to normal form
result
,
_
:=
Reduce
(
expr
,
1000
)
fmt
.
Println
(
"Result:"
,
ToInt
(
result
))
// Output: Result: 3
}
// Example using backslash notation
func
ExampleParse_backslash
() {
// Backslash can be used instead of λ
expr
,
_
:=
Parse
(
"
\\
x.
\\
y.x"
)
fmt
.
Println
(
expr
)
// Output: λx.λy.x
}
Back
|
FazBrowse Home
|
New Git URL