I'm new to Prolog and I'm having a hard time using a dynamic predicate.
First, here's the code I'm executing
:- dynamic(list/1).
add(X, LL) :- asserta(list([])), asserta(list(X)), retract(list(LL)).
I know the code looks weird, but I'm simply looking for the right syntax to use.
Now, if I do :
add(2, LL).
Answer will be :
LL = 2 ;
LL = [].
But what I want to do is to add the X (2) INTO the array ([]). So..
LL = [2].
It looks simple (probably is to), but I can't get it work.
Thanks a lot.
If you want to add X to the front of the list:
add(X, LL) :-
( retract(list(Prev))
-> LL = [X|Prev]
; LL = [X]
),
asserta(list(LL)).
But I agree with #jschimpf's advice. Assert/retract should only be used under certain circumstances as may be quite in efficient in some applications.
Related
I'm trying to make it easier to read some SQL code where we need to hardcode in some large numbers
I'd like to do something like this:
SELECT 3_800_000
Which, according to this post, is really treated like this:
SELECT 3 _800_000
SELECT 3 AS [_800_000]
JS allows numeric separators
let x = 1000000000000
let y = 1_000_000_000_000
console.log(x==y) // true
Also, C# added digit separators in 7.0 as well
// These are equivalent.
var bigNumber = 123456789012345678;
var bigNumberSplit = 123_456_789_012_345_678;
Is something similar possible in T-SQL?
Note: I'm not looking for a way to format the output, I'm looking for a way to make the source code easier to read for big numbers
The answer is, unfortunately (and as commenters pointed out, thanks folks), that this is not currently a feature of T-SQL.
I was not aware of those JS and C#, so thanks for that.
My input text might have a simple statement like this:
aircraft
In my language I call this a name which represents a set of instances with various properties.
It yields an instance_set of all aircraft instances in this example.
I can apply a filter in parenthesis to any instance_set:
aircraft(Altitude < ceiling)
It yields another, possibly reduced instance_set.
And since it is an instance set, I can filter it yet again:
aircraft(Altitude < ceiling)(Speed > min_speed)
I foolishly thought I could do something like this in my grammar:
instance_set = expr
expr = source / instance_set
source = name filter?
It parses my first two cases correctly, but chokes on the last one:
aircraft(Altitude < ceiling)(Speed > min_speed)
The error reported being just before the second open paren.
Why doesn't Arpeggio see that there is just a filtered instance_set which is itself a filtered instance set?
I humbly submit my appeal to the peg parsing gods and await insight...
Your first two cases both match source. Once source is matched, it's matched; that's the PEG contract. So the parser isn't going to explore the alternative.
But suppose it did. How could that help? The rule says that if an expr is not a source, then it's an instance_set. But an instance_set is just an expr. In other words, an expr is either a source or it's an expr. Clearly the alternative doesn't get us anywhere.
I'm pretty sure Arpeggio has repetitions, which is what you really want here:
source = name filter*
I have a DataFrame structured as parName|region|year, and access function as getData(parName,reg,year) ( I use access function because I implement my own query logic).
Would it be possible, based on unique(df[:parName]), to dynamically create a set of functions like par1(region,year) "pointing to" getData("par1",region,year) ?
If so, using which approach?
This is a bit the opposite of this question.. there it is explained how to dynamically call a function, while I wander if it possible to dynamically declare/define one..
EDIT:
I am using this approach in order to get the cleanest and most compact syntax possible in writing multi-dimensional equations.
I managed (thanks to the #Liso answer) to implement it as:
for par in unique(dropna(df[:parName]))
#eval ($(Symbol("$(par)_"))) = (r,d1,d2="",y=-1,op=sum) -> gd($par,r,d1,d2,y,op)
#eval ($(Symbol("$(par)!"))) = (v,r,d1,d2="",y=-1) -> sd(v,$par,r,d1,d2,y)
end
i.e., I am using the convention that par!() is a setData-type and par_() is a getData-type equation.
When I'll be able to complete the macro that transforms f(dim1,dim2) = value into f(value,dim1,dim2) I will be able to write my model using a LaTeX-like (and AMPL-like) syntax that is very clear:
#meq price!(tp in secProducts, r in fr2) = sum(price_(r,pp,"",y2)*a_(r,pp,tp,y2) for pp in priProducts) + margin_(r,tp,"",y2)
I am just beginner trying to understand Julia, so I am not sure if it is good idea or not!
See https://docs.julialang.org/en/stable/manual/metaprogramming/#Code-Generation-1 .
I was able to adapt that example to this :
julia> for i in 4:6
#eval ($(Symbol("func$i")))(a) = a^$i
end
julia> func4(2), func5(2), func6(2)
(16, 32, 64)
Maybe it could help you to play and learn :)
To me, adding "Dump" to the end of an expression doesn't seem to do anything different, at least for seeing rows in a table. Can you point me to an example of where it is handy?
If you are just working with an expression, there is no reason to call Dump—it's called automatically. But, in the language selection box, LINQPad allows allows the selection of Statements and Program. Once you select one of those, you don't get any Dump output unless you call it.
With Statements or Programs, you might want to call Dump on multiple times. In those cases, it is handy to pass the Description parameters so you can distinguish the output.
There are also other parameters you can use to shape the output, such as depth, which limits the substructure details.
Simple example (Language=C# Statements):
var integers = Enumerable.Range(1,10);
integers.Select(i => new { i, v = i * i}).Dump("Squares");
integers.Select(i => new { i, v = i * i * i}).Dump("Cubes");
var output = "λ is awesome";
Encoding.UTF8.GetBytes(output)
.Dump("UTF-8");
Encoding.GetEncoding("Windows-1252").GetBytes(output)
.Dump("Windows-1252 (lossy)");
I'm a beginner in Prolog and I am dealing with a problem that might seem stupid to you, but I really can't understand what I'm doing wrong! Ok, I have this file fruits.pl and inside that I have something like this:
fruit(apple,small,sweet).
fruit(lemon,small,nosweet).
fruit(melon,big,sweet).
I have already (inside that file made a coexist(X,Y) atom that checks if two fruits can be put together in a plate. It works fine! But now I can't create a suggest(X) that takes as a parameter a fruit and returns a list of fruits that can be put together in the same plate.
The thing is I was trying to make something like that
suggest(X) :- findall(Y,fruit(Y,_,_), List), coexist(X,Y).
What do you think? Every time I try to run this in swi prolog there is a warning 'singleton variable' and when I press
suggest(apple).
then it says false..
sorry for my english :/
Predicates in Prolog do not return anything. You have goals that are satisfied or not and you can interpret that as returning true or false.
Your predicate suggest(X) should contain another parameter that will be bound to the list of fruits that go together with X. An option would be: suggest(X, List) which describes the following relation: List represents all the fruits that go together with X. Then, you could ask:
?- suggest(apple, List).
List = [pear, cherry].
The goal findall(Y, ... , ...) uses the Y variable internally and Y is still unbound after the goal is satisfied. So, you should move coexist(X,Y) inside the second argument of findall/3 which is the goal that is satisfied in all possible ways. Th rule below works only if X is instantiated (suggest(+X, -List)).
suggest(X, List) :- findall(Y, (fruit(Y,_,_), coexist(X, Y)), List).
You can read this as follows: "List represents all fruits Y that coexist with X".
When you try to define a predicate in Prolog, first of all pretend that you have written that predicate already and start with imagining how you would use it. That is, what queries you would like to pose.
To me, it looks as if coexist/2 already describes what you want. BTW, may_coexist/2 might be a more descriptive name. Why do you want this in a separate list? And why using fruit/3 at all? But for the sake of the question let's assume that this makes sense. So essentially you would have now a relation fruit_compatible/2:
fruit_compatible(F, G) :-
fruit(F, _, _),
may_coexist(F, G),
fruit(G, _, _). % maybe you want to add this?
And now, let's assume you want this list too. So you would have a relation fruit_suggestions/2. How to use it?
?- fruit_suggestions(apple, L).
L = [cherry,pear].
or ... should it be rather L = [pear,cherry]? Or both?
?- fruit_suggestions(lemon, L).
L = [orange].
So every time I want a suggestion I have to think of a fruit. Always thinking: what fruit should it be? Fortunately there is a less demanding way in Prolog: Simply use a variable instead of the fruit! Now we should get all suggestions at once!
?- fruit_suggestions(F, L).
F = apple, L = [cherry, pear]
; F = lemon, L = [orange]
; F = cromulon, L = [embiggy, mushfruit].
So we need to implement it such that it will behave that way. findall/3 alone does not solve this. And implementing it manually is far from trivial. But there is setof/3 which handles variables in exactly that manner. Many of the tiny nitty-gritty design decisions have already been made, like that the list will be sorted ascendingly.
fruit_suggestions(F, L) :-
setof(G, fruit_compatible(F, G), L).
Edit: Due to the discussion below, here would be a solution that also permits empty lists. Note that this sounds trivial but it is not. To see this, consider the query:
?- fruit_suggestions(F, []).
What does it mean? What should F be? Also things that are no fruits at all? In that case we would have to produce solutions for everything. Like F = badger ; F = 42 ; .... Most probably this does not make much sense. What might be intended is those fruits that are incompatible with everything. To this end, we need to add a new rule:
fruit_suggestions(F, []) :-
setof(t,X^Y^fruit(F,X,Y),_),
\+ fruit_compatible(F, _).
fruit_suggestions(F, L) :-
setof(G, fruit_compatible(F, G), L).