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
Related
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 need help reversing bits in F# as done in this question Reverse bits in number. I'm new to F# and was wondering how we can do this?
let bitreverse x =
let mutable b = 0
while x do
b >>>= 1
b|= x & 1
x >>>= 1
b
I'm not even sure the syntax is correct here. I am very knew to this language.
The direct translation into F# looks like this:
let bitreverse x =
let mutable x = x
let mutable b = 0
while x <> 0 do
b <- b <<< 1
b <- b ||| (x &&& 1)
x <- x >>> 1
b
This is highly imperative with mutable values and this isn't usually how we'd tend to go about writing code in F#. Notice that re-assignment of a mutable variable is a little different to what you might be used to in an imperative language, you have to use <- which is called the destructive update operator.
Thankfully, it's pretty straightforward to translate this into a recursive function that uses immutable values which should be a little more idiomatic
let bitreverse2 x =
let rec bitRerverseHelper b x =
match x with
|0 -> b // if 0, the recursion stops here and we return the result: b
|_ -> bitRerverseHelper ((b <<< 1) ||| (x &&& 1)) (x >>> 1) // otherwise recurse
bitRerverseHelper 0 x
F# doesn't support compound assignment, so you can't do something like b |= x & 1, you need to expand it to b <- b ||| (x &&& 1).
The argument x isn't mutable, so you need to create a local binding and mutate that. It looks weird, but you can just write let mutable x = x as the first line of your function to shadow the existing binding with a mutable one.
x is an int, not a bool, so you can't use it as the condition for your while loop. Use x <> 0 instead.
Indentation matters in F#, so make sure that while and your final b both line up with the first let.
Fixing those issues will make your code work, but idiomatic F# would probably forgo the while loop and mutation and use a recursive inner function with an accumulator instead.
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")
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|]
i would like to add 1 items to a list which has only one item and add items(after using toInt to convert to integer) in a list y if number of items greater than 1 and the last items are the same
How to do?
import Data.List.Split
z = splitOn "+" "x^2+2*x^3+x^2"
y = map (splitOn "*") z
x = map head y
toInt :: [String] -> [Int]
toInt = map read
u = filter ((map length y)>1) y
Couldn't match expected type `a0 -> Bool' with actual type `Bool'
In the first argument of `filter', namely `((map length y) > 1)'
In the expression: filter ((map length y) > 1) y
In an equation for `u': u = filter ((map length y) > 1) y
Failed, modules loaded: none.
Your definition of u is obviously bad. It helps if you give type signatures, so we understand a little better what you are trying to do (even when you don't tell us in words).
You commented that you want all lists of length > 1, this is the same as getting all non-null lists after dropping the first element. So use filter, which tests each element separately (so you don't need map), and build a function that either tests a single list for length > 1 or it's sublist for null:
-- Use the O(n) length for your filter
u = filter ((> 1) . length) y
-- Or use an O(1) drop + null test
u' = filter (not . null . drop 1) y
Without using function composition (.) these functions are:
u = filter (\sublist -> length (sublist) > 1) y
u' = filter (\sublist -> not (null (drop 1 sublist))) y
The compiler is telling you that map length y > 1 is a boolean value, but filter wants a function there. I am not sure what you really want do to with y, please specify what you expect for different values of y.