I'm reading the F# for C# developers book and there's this function that I can't seem to understand what are the effects of this function
let tripleVariable = 1, "two", "three"
let a, b, c = tripleVariable
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a,b,c in l do
printfn "triple is (%d,%d,%d)" a b c
the output is
triple is (1,2,3)
triple is (2,3,4)
triple is (3,4,5)
why a, b, c are initialized with tripleVariable? Is it because it was needed in the for loop to know their type (or its type, since it's a Tuple)?
The code snippet is using variable shadowing when defining the variables a, b and c. The variables are first initialized to the values of tripleVariable (line 2), but then they are shadowed by a new definition inside the for loop (line 4).
You can think of these as different variables - the code is equivalent to the following:
let tripleVariable = 1, "two", "three"
let a1, b1, c1 = tripleVariable
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a2, b2, c2 in l do
printfn "triple is (%d,%d,%d)" a2 b2 c2
Variable shadowing simply lets you define a variable with a name that already exists in the scope. It hides the old variable and all subsequent code will only see the new one. In the above code snippet, the old (shadowed) variables b and c even have different types than the new ones.
The code contains 2 samples. The first one is
let tripleVariable = 1, "two", "three"
let a, b, c = tripleVariable
The 2nd one
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a,b,c in l do
printfn "triple is (%d,%d,%d)" a b c
They can be run independently.
The values a, b, and c in the for loop hide the a, b, and c defined outside of the loop. You can print a, b, and c after the loop to see that they still contain the values from tripleVariable:
let tripleVariable = 1, "two", "three"
let a, b, c = tripleVariable
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a,b,c in l do
printfn "triple is (%d,%d,%d)" a b c
printfn "tripleVariable is (%A,%A,%A)" a b c
Result:
triple is (1,2,3)
triple is (2,3,4)
triple is (3,4,5)
tripleVariable is (1,"two","three")
Related
I get a IEnumerable of object from using a SqlCommandProvider. Those objects match my rows and columns. Lets called it B. In my program I created a type matching those columns too. Lets call this record A. How can I actually transform/cast/construct a record c of type A based on values from B?
type A = {k: int; c: string}
let a:A = { k = 1; c = "c" }
let b = {| k = 1; c = "c" |} // lets say that this one simulate the sql's object
let c:A = b
printfn "%A" a
printfn "%A" b
printfn "%A" c
Try it online!
I got an error of type mismatch:
error FS0001: This expression was expected to have type 'A' but here has type '{|c : string ; k : int|}'
Full sample code with the provider:
type A = {k: int; c: string}
let toA c =
(A)c // ???
do
use cmd = new SqlCommandProvider<"
SELECT k, c
FROM Examples
" , connectionString>(connectionString)
cmd.Execute() |> Seq.map toA
sql table if it matters:
k
c
0
a
1
b
I read the doc without finding a solution to my problem. Maybe it is a comprehension problem.
A working solution is to construct a record from the object:
let toA c =
{k = c.k; c = c.c }
cmd.Execute()
|> Seq.map toA
For the example, how to write code in z3py to maximize value of a+b+c+d as a,b,c,d are all chosen from list [1,2,3,4,5,6,7,8], which is given as the input. a, b, c, d are distinct values.
a, b, c, d = Ints("a b c d")
o = Optimize()
list = [1,2,3,4,5,6,7,8]
o.maximize(a+b+c+d)
how to write code corresponding to choose "a b c d" value from list. The correct out value of a+b+c+d is 26
Thanks!
Here's one way:
from z3 import *
a, b, c, d = Ints("a b c d")
o = Optimize()
list = [1,2,3,4,5,6,7,8]
vars = [a, b, c, d]
for v in vars:
o.add(Or([v == e for e in list]))
o.add(Distinct(*vars))
goal = Int("goal")
o.add(goal == a+b+c+d)
o.maximize(goal)
if o.check() == sat:
print o.model()
else:
print "not satisfiable"
When I run this, I get:
$ python a.py
[d = 5, c = 8, b = 6, a = 7, goal = 26]
Can I create a list with atoms as reference for them to later use it in my move method?
createLists(X) ->
List = [
listA = lists:seq(1, X),
listB = [],
listC = []
],
List.
Then I create like T = hello:createLists(10).
move(List, A, B) ->
...
How can I obtain A and B using atoms? I'm pretty new to Erlang so the answer might be obvious.
move is supposed to move top element of the from A to B but I struggle to pass A and B when I do like hello:move(List, ?, ?).
Regarding the code in your comment, #armedor:
1 move(List, a, b, c) ->
2 Source = proplists:get_value(a, List),
3 Dest = proplists:get_value(b, List),
4 Help = proplists:get_value(c, List),
5 Temp1 = [lists:nth(1,Source)],
6 NewDest = [lists:append(Dest,Temp1)],
7 NewSource = lists:subtract(Source,Temp1),
8 List1=[NewSource,NewDest,Help].
Say List is defined as [ {a, lists:seq(1, 3)}, {b, []}, {c, []} ].
Line 8 isn't going to be a proplist anymore, but just the sublists themselves. Instead, create List1 as [{a, NewSource}, {b, NewDest}, Help].
You also don't need to assign to List1 in line 8, since the function is returning - just make the new list itself the last line, which will be the return value.
And the lists:nth and lists:subtract you're doing can be combined as
[Temp1 | NewSource] = Source
I just tried to create a histogram build upon a list of characters like this:
histogram [a c a c b c a c c c c c c a c c a c a c a a c c c a a b c b c]
But it doesn't show anything.
Is it just supposed to handle numbers or am I missing something?
You didnĀ“t use quotes in your histogram list, but I am assuming that you wanted to plot a list of strings like ["a" "b" "c" ...], right?
As far as I know, it is not possible to use categorical values (like strings) for a histogram in netlogo plots. This is also stated in the netlogo dictionary:
histogram [...] Any non-numeric values in the list are ignored. [...]
One way to solve this would be a conversion to a numeric list by just giving every string-character a specific number:
let m ["a" "c" "a" "c" "b" "c" "a" "a" "c" "c" "b" "b" "c" "c" "a" "a"]
let n [ ]
foreach m
[
if (? = "a") [set n lput 0 n]
if (? = "b") [set n lput 1 n]
if (? = "c") [set n lput 2 n]
;...
]
histogram n
histogram work as you said. It's not easy to help you with as little information about your code. For me the error can come from 2 ways :
the plot context definition need to be in bar (i.e image)
the x-range plot definition need to be defined
for the 2nd hypothese some thing like
set-plot-x-range 0 ( (max myliste) + 5)
histogram myliste
let maxbar modes myliste
let maxrange length filter [ ? = item 0 maxbar ] myliste
set-plot-y-range 0 1000
In the plot on code interface may work
If you choose the 2nd ways, you don't need tout defined the scale like on the screenshot
I have two collections (they happen to be arrays, but it doesn't really matter, I think): L and R. They are both sorted and now I want to compare them. I want to end up with two collections: one for each input array containing the items which were not in the other.
I could just take the first item from L and then search R and, if there isn't a match, add it to my "unique" collection (Lu). But that's extremely inefficient, and I am expecting to have some very large collections to process in the near future.
I though about possibly "playing hopscotch":
Step 1: Take two lists, L and R, and compare the head of each list ( l :: L and r :: R):
Branch 1: if l < r, then add l to Lu and recurse, passing in L and r :: R
Branch 2: if l > r, then add r to Ru and recurse, passing in l :: L and R
Branch 3: if l = r, then recurse, passing in L and R
Step 2: return Lu and Ru
I can write this function, but before I put in the effort I was wondering if a function already exists which can do this for me. It seems like a not-to-uncommon scenario, and I'd always rather use an existing solution to rolling my own.
(Also, if there's a more recognizable name for this algorithm, I'd like to know what it's called.)
(I wrote the question above about 2 hours ago. Since then, I found the answer on my own. The following is what I discovered.)
In set theory, the "list" of items in L but not in R is known as "the relative complement of R in L", also known as "set-theoretic difference of L and R"
(See Wikipedia's Complement (set theory) article)
F#, being a mathematical language, has this concept baked right in to it's Core library. First, you need to build your collections as sets:
// example arrays:
let arr1 = [| 1; 2; 3 |]
let arr2 = [| 2; 3; 4 |]
// build the L and R sets
let L = set arr1
let R = set arr2
Now you can call the "difference" function and quickly get the relative complement for each array:
let Lu = Set.difference L R |> Set.toArray
let Ru = Set.difference R L |> Set.toArray
> val Lu : int [] = [|1|]
> val Ru : int [] = [|4|]
There's also a shorter syntax. The Set type has overloaded the minus operator. Set.difference just subtracts the second parameter from the first, so you can actually just use the following:
let Lu = L - R |> Set.toArray
let Ru = R - L |> Set.toArray
> val Lu : int [] = [|1|]
> val Ru : int [] = [|4|]