Calling http rest API and parsing json response with (scryer) #Prolog

2024-03-02 | #programming #prolog

In this simple code I’ll show how you can call a rest api (qliksense saas rest api) and parse its json response


?- use_module(library(dcgs)).
?- use_module(library(pairs)).
?- use_module(library(serialization/json)).
?- use_module(library(http/http_open)).

?- set_prolog_flag(answer_write_options, [max_depth(1000)]).

?- http_open("https://mytenant.eu.qlikcloud.com/api/v1/tenants/me", S, [request_headers(['Authorization'("Bearer MYKEY")])]), 
	get_n_chars(S, _N, HTML),
	phrase(json_chars(pairs(J)), HTML), 
	member(string("id")-string(ID), J).

 HTML = "{\"id\":\"myid\",\"hostnames\":[\"mytenant.eu.qlikcloud.com\"]}", J = [string("id")-string("myid"),string("hostnames")-list([string("mytenant.eu.qlikcloud.com)])], ID = "myid"

Continue reading 


Family Riddle problem with Prolog

2024-01-12 | #programming #prolog

My #prolog solution for “Family Riddle” proposed by dmcommunity.org challenge Jan 2024.

Let’s assume that Rene and Leo are both heads of household, and, what a coincidence, both families include three girls and three boys. The youngest child in Leo’s family is a girl, and in Rene’s family, a little girl has just arrived. In other words, there is a girl in Rene’s family whose age is less than one year. Neither family includes any twins, nor any children closer in age than a year. All the children are under age ten. In each family, the sum of the ages of the girls is equal to the sum of the ages of the boys; in fact, the sum of the squares of the ages of the girls is equal to the sum of the squares of the ages of the boys. The sum of the ages of all these children is 60.

Continue reading 