'check if an element exist in nested tuple with Clingo and appending an element to tuple with python API

I am new to Clingo. so, I am trying to find out how it works. I have two questions regarding Clingo and its python API. first, I have nested tuple such as tuple((1,(2,(3,(4,5))))). the length of the nested tuple may vary and the sequence is random. I want to check if a number for example n exists in the tuple if not do something. I am trying to check it with python API something like the code below obviously my code does not work.

#script (python)

import clingo
def is_in_tuple(x,n):
    if n in x.arguments:
        return True
    else: 
        return False
#end.

tuple((1,(2,(3,(4,5))))).
h(X) :- tuple(X), @is_in_tuple(X,2).
#show h/1.

my other question is about appending to a simple tuple in python API for example we have tuple((1,2,3,4)) in Clingo and I want to append 0 to its beginning so the answer must look like this ((0,1,2,3,4)). I want to use something like the code below. but the code will return the first element and 0. is it even possible to change the tuple like this? Thanks in advance.

#script (python)
import clingo

def append(x,n):
    return [n, *x.arguments]
#end.

tuple(c(1,2,3,4,5)).
h(@append(X,0)) :- tuple(X).
#show h/1.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source