I've created a concatenate/1 function, which should concatenate a list of lists and atoms:
concatenate(L)->
concatenate(L,[]).
concatenate([],R)->
reverse(R);
concatenate([H|T],R) when is_atom(H) ->
concatenate(T,[H|R]);
concatenate([[]|L],R)->
concatenate(L,R);
concatenate([[H|T]|L],R)->
concatenate([T|L],[H|R]).
However I get the error:
15> ml:concatenate([[1,2],4,[3,4,5],[5,6]]).
** exception error: no function clause matching
ml:concatenate([4,[3,4,5],[5,6]],[2,1]) (ml.erl, line 27)
which I believe should match, because of the second concatenate/2 clause.
May I ask, what am I doing wrong here?
Following the reasoning in Viacheslav Kovalev's answer, given that I want to handle atoms and numbers here, I have come up with the following solution:
concatenate([],R)->
R;
concatenate([H|T],R) when not(is_list(H)) ->
reverse(concatenate(T,[H|R]));
concatenate([[]|L],R)->
concatenate(L,R);
concatenate([[H|T]|L],R)->
concatenate([T|L],[H|R]).
In the second clause you are using when is_atom(H) guard, which fails to match clause (because H in this case is number). Change it to when is_number(H).
Related
I write a keyword like this:
My Keyword
[Argument] ${param}
Internal Keyword 1
...
Internal Keyword n
Run When Any of The Upper Keywords Failed
I want the keyword run as its' description, who can tell me how to achieve it?
There are a few ways to achieve this, one is to use Run Keyword And Return Status - it returns a boolean true/false did the wrapped keyword pass or fail. And based on that, you can run the "exception" keyword:
${passed 1}= Run Keyword And Return Status Internal Keyword 1
${passed 2}= Run Keyword And Return Status Internal Keyword 2
Run Keyword If not ${passed 1} or not ${passed 2} Run When Any of The Upper Keywords Failed
The argument I'm looking for is, if the Members.Status field is equal to LA and the Member.Isresident field is False then the Members.Locality field will fill "LOST" in that field. I attempted to write it this way and am receiving an error of invalid syntax.
Locality: Iif ([Members.status] = "LA" and ([isresident] "False", [members.locality], "LOST")
The parentheses in your example are unbalanced. There are two ( but only one ).
Add an equal sign between [isresident] and "False". And if isresident is Yes/No data type, eliminate the quotes around False.
Re-using the field name as the alias for an expression can get you into trouble. You can avoid trouble there with a different alias such as adjusted_locality instead of Locality. But if you prefer to keep Locality as the alias, bracket it as in the example below.
Since I don't know about the context where you're attempting to use that expression, I'll suggest you try this simple SELECT to work out the syntax of the IIf expression.
SELECT
IIf(m.Status="LA" And m.isresident=False, m.locality, "LOST") AS [Locality]
FROM Members AS m;
You can create a new query, switch to SQL View, paste in that SELECT statement, and then run it to see whether any errors remain.
I am struggling to formulate the right sql-query to access the property of an object stored in one of my columns and use it as an input for a function:
I have a table 'search'
in 'search' one column is named 'coord' and stores POINT-Objects
I defined a function 'test' which gets a double-input parameter and just returns the input parameter as double
I want to make a query such as
SELECT test('coord.X') as res FROM `search` ORDER BY res
Expected result: The coord.X values listed in a columns named 'res' ordered from lowest to highest.
What I get is '0' for every entry (the coord X/Y values are of course set to arbitrary values).
If I leave the coord.X without the "'" around it the query fails with an error
#1054 - Unknown column 'coord.X' in 'field list'
I searched a lot but I couldnt find any pages telling me how to access properties of sql-objects. Any help is very much appreciated.
Consider your sample query without the test function.
SELECT coord.X as res FROM `search` ORDER BY res
Sql views coord.X as a column name and looks for that column in search. Since X is a property of coord, rather than part of the column name, this is what was causing your error.
Using
SELECT test(X(coord)) as res FROM `search` ORDER BY res
Should yield the correct results. Just as the syntax to call your test function is test(), the syntax to call the X function (defined in spacial extensions) is X(). However, this function will not work on columns that are not explicitly declared as a Point type because sql will not know how to parse the value and return the specified information.
Essentially, X() is similar to the many other mysql functions, such as ISNULL() or SUM(). Just as SUM() expects int data types as input, X() expects Point data types.
Shouldn't the query be:
SELECT coord as res FROM search ORDER BY res
Maybe I'm not understanding your question correctly.
I have the following query in MS-Access 2003 and it works OK:
SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent, tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (tblDiscounts.DiscountID) = IIf ([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3,4)));
I wish to replace the IIf function with the Switch function but whatever I tried didn't work. My best approach is the following:
SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent, tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (((tblDiscounts.DiscountID)=SWITCH ([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]>3,4)));
but I get a message
Type mismatch in expression
Please advise!
One difference I can see is that if [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]<1 the nested IIfs will return 4 while the Switch statement will return Null. Check your underlying data to see if that could happen; a Null value might very well mess up the WHERE clause.
I am getting the following error on production server. It works well on localhost.
Error: Sequence contains more than one element
That is an InvalidOperationException thrown by the Single method.
This method is supposed to return only one element, and your query is returning more than one, you have to check the criteria you use on your query.
It will also throw an exception if it doesn't find an element. You can use SingleOrDefault() method to return null (or default value, i.e. 0 for int) if there are no records.
If you are expecting multiple elements but just one the first one, you can use First instead of Single or FirstOrDefault.