I have proc A, that have three parameters, and proc B that have a flexible number of parameters, since proc B could be anything. My problem is how to invoke proc B from within proc A. Currently I am doing:
proc B {a b c d to n} {
do something that is right since I can call B individually and it works
}
set listB [list B a b c d to n]
proc A {a b listB} {
do something
if {$listB != 0} {
execute proc B
}
}
The problem is that I cannot execute B. In order to execute B I have tried:
$listB
[set listB]
[join $listB " "]
[concat $listB]
All of them fails with the same error because for TCL I am trying to use a command with name
"B a b c d to n"
What I am trying to achieve is the same that works here:
bind $uf.infid <Return> [list B a b c d to n]
-- UPDATE --
I should have mentioned that the name of proc B also can change, since I am trying to make proc A as general as posible. That is why the name of proc B is the first element in the list used as argument for proc A. Nevertheless
{*}$listB
does the job. Thanks Brad Lanam!!!
proc A accepts three arguments, two normal and a list.
proc B accepts multiple arguments, not an argument that is a list.
Considering the large number of arguments passed to proc B, I think you
would be better off re-writing proc B to accept an argument that is a list.
In general, if you have a list and you need to pass the individual arguments
to a procedure, you use the expansion operator {*} to expand the list
into a set of individual words.
proc A { a b listB } {
B {*}$listB
}
In older versions of Tcl, the eval operator was required
proc A { a b listB } {
eval B $listB
}
If proc B is rewritten
proc B { mylist } {
foreach {val} $mylist {
...
}
}
proc A { a b listB } {
B $listB
}
(This can get very confusing to talk about, as B has an argument list that
is a list of words, not an argument that is a list, so I have avoided using
list except as a reference to the particular structure.)
Related
I am trying to solve this problem from the 99 lisp problems
Flatten a nested list structure.
Transform a list, possibly holding lists as elements into a `flat' list by replacing each list with its elements (recursively).
Example:
(my-flatten '(a (b (c d) e)))
(A B C D E)
Hint: Use the predefined functions list and append.
Problem is, I don't know how to create that nested list to begin with so I can try to flatten it myself. Please help thankyou
In Lua "a list possibly holding lists as elements" is just a table possibly holding tables as elements.
From the Lua manual:
Tables can be heterogeneous; that is, they can contain values of all
types (except nil).
...
Like indices, the values of table fields can be of any type
That of course includes tables.
The most trivial case is local t = {{}} or for the mentioned example (a (b (c d) e)) local t = {a, {b, {c, d}, e}}
Assuming a,b,c,d and e are strings your list would look like so:
local t = {"a", {"b", {"c", "d"}, "e"}
I am trying to prove the following lemma in Coq:
Require Import Lists.List.
Import ListNotations.
Lemma not_empty : forall (A : Type) (a b : list A),
(a <> [] \/ b <> []) -> a ++ b <> [].
Right now my current strategy was to destruct on a, and after breaking the disjunction I could ideally just state that if a <> [] then a ++ b must also be <> []... That was the plan, but I can't seem to get past a subgoal that resembles the first " a ++ b <> []", even when my context clearly states that " b <> []". Any advice?
I've also looked through a lot of the pre-existing list theorems and haven't found anything particularly helpful (minus app_nil_l and app_nil_r, for some subgoals).
Starting with destruct a is a good idea indeed.
For the case where a is Nil, you should destruct the (a <> [] \/ b <> []) hypothesis. There will be two cases:
the right one the hypothesis [] <> [] is a contradiction,
the left one, the hypothesis b <> [] is your goal (since a = [])
For the case where a is a :: a0, you should use discriminate as Julien said.
You started the right way with your destruct a.
You should end up at some point with a0::a++b<>0. It ressembles a++b<>0 but it is quite different as you have a cons here, thus discriminate knows that it is different from nil.
first, I am not sure which Coq version you are using, the syntax certainly looks odd. Seconds, it is hard for us to help if you don't show us the proof you have so far. I should say that indeed your strategy seems correct, you should destruct both lists, tho it is better if you first inspect the or to see which list is not empty.
Another option is to use computation to show your lemma, in this case, equality will compute and thus you will get the result of the comparison. It suffices to destroy only one list in this case due the order or arguments:
From mathcomp Require Import all_ssreflect.
Lemma not_empty (A : eqType) (a b : seq A) :
[|| a != [::] | b != [::]] -> a ++ b != [::].
Proof. by case: a. Qed.
Given a list of key/value pairs:
do
[("A", 1);("B",2)]
|> (fun list ->
for item in list do
match item with
| ("A", x) -> //expression assigning x to variable "A"
| ("B", x) -> //expression assigning x to variable "B"
| _ -> //default expression
)
Are there some expressions I can write that will accomplish the same thing as
let A = 1
let B = 2
I know in advance that the list has no more than 10 items, and that the items are tuples of <string, int> but don't know in advance whether "A" or "B" or other keys may be present or if the list is be empty.
Basically, decompose the list into a set of typed variables.
UPDATE: Notes on Context
In my app I receive a list of key/value pairs just like this from an upstream program maintained by a different team. I expect certain keys to be in the list, which i want to assign to typed variables for later processing. I'll throw an exception if ones I need are missing. However I don't want to be locked in to any given set of variable names, except for the expected ones. For all of them, I want to be able to assign them at run time.
If you want to do something with the variables later on in the code, then they will need to be in scope - so you will inevitably have to declare the variables in some way. The easiest option is to make them mutable and mutate them according to the values in the list:
let mutable A = 0
let mutable B = 0
To make the mutation a bit easier, you can create a lookup table with setters:
let setters =
dict [ ("A", fun x -> A <- x); ("B", fun x -> B <- x) ]
This now lets you iterate over the inputs, use dictionary lookup to get the appropriate setter and invoke it to set the variable value:
for var, value in [("A", 1);("B",2)] do
setters.[var] value
With the limited context of your question, it is a bit hard to say whether this is a good way of achieving what actually motivated your question - as F# code, it does look like a bit ugly and it is likely that if you add more context, someone can suggest a better way.
It sounds like you have some dynamic data that can be represented well as a map:
let data = Map [ ("A", 1); ("B", 2) ]
let a = Map.find "A" data
// throws an exception if "A" is not present
let b = Map.find "B" data
// throws an exception if "B" is not present
let c = Map.tryFind "C" data
// "C" is not present so c = None
Note: In F# the convention is for binding names (variable names) to start with a lower case letter.
in a Spark process I have an RDD[Try[(A, B)]]. I have to transform this RDD using a function f: B => List[C]. What I want to obtain is an RDD[Try[(A, B, C)], in which I have to flatMap the list obtained from the application of function f.
I tryed this:
val tryRdd = // Obtain the RDD[Try[(A, B)]]
val transformedRdd =
tryRdd.map {
pair =>
for {
(a, b) <- pair
c <- f(b)
} yield {
(a, b, c)
}
}
Unfortunately what I am obtaining is an RDD[Try[Nothing]]. Why? Can anyone help me to understand where I am wrong?
I suppose that problem is not really related to RDD. Probabily RDD with List will end with the same result.
The for-comprehension is translated to
pair.flatMap { case (a, b) => f(b).map { case c => (a, b, c) } }
But f(b).map(...) will give you a List[(A, B, C)], not a Try[(A, B, C)] which you want for the argument of pair.flatMap. So the code shouldn't compile at all (unless you have a strange implicit conversion in scope).
But if you are using, say, IntelliJ, it can fail to show the error and show an incorrect type (or the other way around, it can show errors in working code): you need to actually build the project to see the real errors.
Have you tried to formally type your RDD ?
val transformedRdd : RDD[Try[Tuple3]] = ...
Edit :
If this does raise you an error, then the output of the map is wrong.
The pair variable's type is Try.
Since scala doesn't do it for you, you must add some instruction to interact with its content (the tuple (A,B)).
Plus, you don't have to keep the Try type.
I would use a flatMap to keep successes and clean my RDD.
Something like
val transformedRdd = tryRdd.flatMap {value =>
value match {
case Success((a,b)) => ...
}
}
Watch http://www.scala-lang.org/api/2.9.3/scala/util/Try.html for more informations about the Try class.
I'm trying to write a simple Ocaml function but im getting this error:
Error: This expression has type unit
but an expression was expected of type int
let rec euclid a b =
if a = b then a
else if a < b then 1
else if a > b then 2
To fix the immediate problem, you need else clauses in your function:
let rec euclid a b =
if a = b then a
else if a < b then 1
else 2 (* You know a > b is the only possible alternative *)
You may realize this, but this function is not recursive, nor, I think, is it doing what you want it to do.
However, there is an error in the way you're conceptualizing how a function works in Ocaml. The way you've written the function is imperative in nature; it is a series of if/then statements which are acted upon sequentially. Rather, the return value of euclid should be simply the result of one broad if/then statement (an integer value). Nesting, as I have done above, can be acceptable, but the essential thing to take away is that a function is just a single expression which is evaluated, not a series of imperative actions.
EDIT for updated question:
All OCaml if/then statements should have else clauses. Your very last nested if/then statement has no else clause. If OCaml detects an if/then statement with no else clause, an else clause is assumed returning () (unit). Essentially, if a > b is false, what should OCaml return? It assumes nothing, but returning () conflicts with the supposed type of your function (an integer).
Of course, that a > b is false is impossible in your function, since if not a = b and not a < b, the only other choice is a > b. Thus, you don't need another if statement at the end of your function; at that point, you know without a doubt that a > b, so you can simply say else 2.