-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Working with lists #160
Comments
@riccardopinosio Hi, I think the first problem boils down to
|
On the 2nd thing, as far as I know, there's no standard predicate to do the sum of elements. I think this answer matches your requirement: https://stackoverflow.com/revisions/12413025/2 |
Regarding the 3rd point, yes, you can return lists into Go! Here's how you do it: https://go.dev/play/p/ZLT0n98z8JG package main
import (
"fmt"
"github.com/ichiban/prolog"
)
func main() {
p := prolog.New(nil, nil)
sol := p.QuerySolution(`Atoms = [foo, bar], Integers = [1, 2], Floats = [1.0, 2.0], Mixed = [foo, 1, 1.0].`)
var s struct {
Atoms []string
Integers []int64
Floats []float64
Mixed []interface{}
}
if err := sol.Scan(&s); err != nil {
panic(err)
}
fmt.Printf("Atoms = %s\n", s.Atoms)
fmt.Printf("Integers = %d\n", s.Integers)
fmt.Printf("Floats = %f\n", s.Floats)
fmt.Printf("Mixed = %s\n", s.Mixed)
} |
@ichiban cool! Would the example with returning lists also be a good candidate for the /examples folder? |
Regarding predicate names, I highly recommend the SICStus API of https://sicstus.sics.se/sicstus/docs/4.6.0/html/sicstus/lib_002dlists.html |
@riccardopinosio I think this should be also a testable example so that we can know when we break it accidentally. |
The example above #160 (comment) is in the package documentation now https://pkg.go.dev/github.com/ichiban/prolog#example-Solutions.Scan-List |
Now that we've added package main
import (
"fmt"
"github.com/ichiban/prolog"
)
func main() {
p := prolog.New(nil, nil)
if err := p.Exec(`
is_comparison(a).
comparison_list(a, [1.0, 2.0]).
score(X,Y) :- is_comparison(X), comparison_list(X,Z), nth0(0, Z, Y).
`); err != nil {
panic(err)
}
// Prolog program invocation takes a form of query.
sols, err := p.Query(`score(Comparison, Score).`)
if err != nil {
panic(err)
}
defer sols.Close()
// Iterates over solutions.
for sols.Next() {
// Prepare a struct with fields which name corresponds with a variable in the query.
var s struct {
Comparison string
Score float64
}
if err := sols.Scan(&s); err != nil {
panic(err)
}
fmt.Printf("(%s, %.1f)\n", s.Comparison, s.Score)
}
}
|
I'm going to close this issue for now. Feel free to reopen it if anything unclear! |
Hi, I'm trying to experiment more with lists in the library but I'm struggling a bit, maybe you can help.
Given the program:
I would expect the query
score(Comparison, Score)
to return a pair (a, 1.0) in the below code:but it doesn't seem to return anything. Am I making a mistake here? Is the usage of the nth predicate correct?
Second thing on lists. I would like to calculate a sum of elements in a list of numbers. I see it's not a builtin currently, so I assume I could just define it as a custom predicate using recursion (like her https://stackoverflow.com/questions/9875760/sum-of-elements-in-list-in-prolog).
Third thing on lists: is it possible to return a prolog list into go? How would I specify the types in the struct?
Thanks!
The text was updated successfully, but these errors were encountered: