Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

277
Visualizações
Tau Prolog - how to convert a prolog list into a JavaScript array

I want to use Tau Prolog together with JavaScript on a Node Server, following this tutorial, which works well. I need to convert the answers in a JavaScript compatible format, especially lists to arrays, or objects and vice versa.

I changed the Prolog program and goal in a way that it returns a list

Program: test(t, [64,65,100,120]).

Goal: test(t, X).

which returns with

console.log(session.format_answer(answer));

X = [64,65,100,120]

too the console. How can I get only the list and assign it to a js variable?

I tried

answer.lookup("X")

which gives me

Term {
ref: 1258,
id: '.',
args: [
Num { is_float: false, value: 64 },
Term { ref: 1257, id: '.', args: [Array], indicator: './2' }
],
indicator: './2'
}

Which is not very comfortable to access. The args array seems to be the list. I managed to get single list elements with

console.log(answer.links.X.args[1].args[1].args);

to get the third list element.
What is the best way to use complex answers and lists in JavaScript?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Just for completeness, I replicate this answer here.

You can write a function to transform Prolog lists to arrays:

function fromList(xs) {
    var arr = [];
    while(pl.type.is_term(xs) && xs.indicator === "./2") {
        arr.push(xs.args[0]);
        xs = xs.args[1];
    }
    if(pl.type.is_term(xs) && xs.indicator === "[]/0")
        return arr;
    return null;
}

Example:

var session = pl.create();
session.query("X = [1,2,3].");
session.answer(a => console.log(fromList(a.lookup("X")))); // [ {...}, {...}, {...} ]

Note that elements in array are still Tau Prolog objects.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda