I have to represent [[fruits],[ ] ]
in dot notation form in Prolog, and below is how I did it ,but something tells me this is wrong because I have expanded [[ ]]
too, is this incorrect ?
.([fruits],[[ ]] )
.(.(fruits, []),.([],[])
In addition to what Willem wrote, you can always use write_canonical/1
to obtain the canonical representation of any term.
For example, in your case:
| ?- write_canonical([[fruits],[ ] ]). '.'('.'(fruits,[]),'.'([],[]))
This solves the task, and shows that you have expanded the list [[]]
correctly.
In particular, we have:
| ?- write_canonical([[]]). '.'([],[])
That's right: This is a list with a single element, which is []
as indicated by the first argument of the '.'/2
term. Since it is the only element, the second argument is also []
.
Well, the ./2
is what is in Lisp known as the cons
. It contains two parameters: a head the element, and a tail. The tail can be the empty list []
, or another cons
.
Let us first look at the term we have to convert:
X = [ [fruits] , [] ]
What we see is an outer list with two elements (we will ignore these elements for now). So that means that we have a structure like:
X = .( Item1, .( Item2, []) ).
Now of course we still need to fill in Item1
and Item2
. Item2
is not hard: it is the empty list []
so:
Item2 = [].
Item1
on the other hand is a list with one element, so the structure is:
Item1 = .( Item11, [] ).
With Item11
the item of that sublist. That item is fruits
, so that means that:
Item11 = fruits.
If we put these all together, we obtain:
X = .( .(fruits,[]), .([],[]) ).
If we enter this in GNU-Prolog (gprolog
), we get:
$ gprolog
GNU Prolog 1.4.5 (64 bits)
Compiled Feb 5 2017, 10:30:08 with gcc
By Daniel Diaz
Copyright (C) 1999-2016 Daniel Diaz
| ?- X = .( .(fruits,[]), .([],[]) ).
X = [[fruits],[]]
yes
gprolog
thus can be a tool to do verification, since it will convert the dot notation into the syntactical sugar for a list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With