I need your help again. I was wondering if it is possible to use lists:seq(from,to) within pattern matching? Below is the code that I am trying to achieve
product_selling_price_evaluate(lists:seq(1100,1190),standard_produce,Costprice) -> Costprice*10;
product_selling_price_evaluate(lists:seq(1200,1300),standard_produce,Costprice) -> Costprice*20;
product_selling_price_evaluate(lists:seq(1400,1500),standard_produce,Costprice) -> Costprice*30;
product_selling_price_evaluate(lists:seq(1600,1700),standard_produce,Costprice) -> 40*Costprice.
When I compile the code it gives me a illegal pattern error!
Example input would be
selling_price:product_selling_price_evaluate(1100,standard_produce,10).
and I want it to find the first one as a match and give the output as
100
Will lists:seq work with case?
Let me explain in a more simple way. I have the below cases
1100 to 1190 = Apples
1200 to 1300 = Oranges
1400 to 1500 = Bananas
1600 to 1700 = Berries
if I give the input as 1125 I want the output to be Apples. Again if I give the input as 1450, I want the output to be Bananas. I hope u guys understood, what am I trying to achieve!
If I interpret right what you are trying to achieve, what you probably are looking for are guards.
Something like this (untested):
product_selling_price_evaluate(N,standard_produce,Costprice) when N >= 10 andalso N <= 20 -> Costprice*10;
product_selling_price_evaluate(N,standard_produce,Costprice) when N >= 21 andalso N <= 30 -> Costprice*20;
[...]
You're not giving the desired input, your function is expecting a list([11001,1101, 1102,...,1189,1190]) as its first argument, where as you're giving a number i.e. 1100.
Guards can be comma-separated (,), it has the same meaning same as andalso.
product_selling_price_evaluate(N,standard_produce,Costprice)
when 10 =< N, N =< 20 -> Costprice*10;
product_selling_price_evaluate(N,standard_produce,Costprice)
when 21 =< N, N =< 30 -> Costprice*20;
...
<= is illegal here, use =< instead.
Related
I would like to assert that the most significant digit of a number is a particular value, but I don't actually know the length of the number. If it was the least significant digit, I know I could use the python mod (%) to check for it. But with an unknown number of digits, I'm unsure of how I could check this in z3.
For example, I may know that the left most digit is a 9, such as 9x, or 9xx, or 9xxx etc.
Thanks so much in advance
The generic way to do this would be to convert to a string and check that the first character matches:
from z3 import *
s = Solver()
n = Int('n')
s.add(SubString(IntToStr(n), 0, 1) == "9")
r = s.check()
if r == sat:
m = s.model()
print("n =", m[n])
else:
print("Solver said:", r)
This prints:
n = 9
Note that IntToStr expects its argument to be non-negative, so if you need to support negative numbers, you'll have to write extra code to accommodate for that. See https://smtlib.cs.uiowa.edu/theories-UnicodeStrings.shtml for details.
Aside While this will accomplish what you want in its generality, it may not be the most efficient way to encode this constraint. Since it goes through strings, the constraints generated might cause performance issues. If you have an upper limit on your number, it might be more efficient to code it explicitly. For instance, if you know your number is less than a 1000, I'd code it as (pseudocode):
n == 9 || n >= 90 && n <= 99 || n >= 900 && n <= 999
etc. until you have the range you needed covered. This would lead to much simpler constraints and perform a lot better in general. Note that this'll work even if you don't know the exact length, but have an upper bound on it. But of course, it all depends on what you are trying to achieve and what else you know about the number itself.
I am very new to Erlang and I have a question about this function that returns a list counting down in ones. e.g. [3,2,1]
Why doesn't it work if you change the N-1 to N-2?
I'm looking to get the output to show [7,5,3,1]
-module(create_list).
-export([create_list/1]).
create_list(0) ->
[];
create_list(N) ->
[ N | create_list(N - 1) ].
Because no matter how many times you subtract 2 from an odd number, you'll never get zero?
I've never even seen Erlang code before, but I suspect the fix is to add
create_list(1) ->
[ 1 ];
Safer to use positive check:
create_list(N) when N > 0 ->
[N | create_list(N - 2)];
create_list(_) ->
[].
There is already a function to do this:
lists:seq(From, To, Incr) -> Seq
1> lists:seq(7,1,-2).
[7,5,3,1]
I'd like to rewrite this Parallel.For loop using a PLINQ method AsParallel(). As far as I have seen, AsParallel() requires passing an entire sequence to it, eg create Array A, then call A.AsParallel().ForAll or A.AsParallel().Select . Is there a way to make AsParallel() work on a range - in this case, from low to high?
for k = 2 to m+n do
let low = max 1 (k-m)
let high = min (k-1) n
Parallel.For(low, high + 1, (fun j ->
One way to do this would be to use ParallelEnumerable.Range():
ParallelEnumerable.Range(low, high-low+1).Select(fun i -> let a = A.[i] in …)
You can use ParallelEnumerable.Skip and .Take to skip a number of elements, then take a certain number, which essentially allows you to query a range.
This will be A.AsParallel().Skip(low).Take(high-low+1).Select(...
I'm sure that there is a function for that. I just want to make a list of 1000 numbers, each one of them which should be random.
To generate a 1000-element list with random numbers between 1 and 10:
[rand:uniform(10) || _ <- lists:seq(1, 1000)].
Change 10 and 1000 to appropriate numbers. If you omit the 10 from from the rand:uniform call, you'll get a random floating point number between 0.0 and 1.0.
On Erlang versions below 18.0: Use the random module instead. Caution! You need to run random:seed/3 before using it per process, to avoid getting the same pseudo random numbers.
Make sure to seed appropriately.
> F = fun() -> io:format("~p~n", [[random:uniform(10) || _ <- lists:seq(1, 10)]]) end.
> spawn(F).
[1,5,8,10,6,4,6,10,7,5]
> spawn(F).
[1,5,8,10,6,4,6,10,7,5]
Your intuition is that the results would be different. A random seed in Erlang is process specific. The default seed is fixed though. That's why you get the same result even though there are two processes in the example.
> G = fun() -> {A1,A2,A3} = now(),
random:seed(A1, A2, A3),
io:format("~p~n", [[random:uniform(10) || _ <- lists:seq(1, 10)]])
end.
> spawn(G).
[3,1,10,7,9,4,9,2,8,3]
> spawn(G).
[9,1,4,7,8,8,8,3,5,6]
Note that if the return value of now() is the same in two different processes you end up with the same problem as above. Which is why some people like to use a gen_server for wrapping random number generation. Alternatively you can use better seeds.
i will be more then happy to get also a site that i will be able to
read it there. thanks.
You should check out Learn You Some Erlang which will guide you through the language.
Pseudorandom number generator from crypto module works better crypto:rand_uniform(From, To).
To generate a 1000-element list with random numbers between 1 and 10:
crypto:start(),
[crypto:rand_uniform(1, 10) || _ <- lists:seq(1, 1000)].
From Erlang Central wiki:
http://erlangcentral.org/wiki/index.php?title=Random_Numbers
Where N = no of items, StartVal = minimum value and Lim = maximum value
generate_random_int_list(N,StartVal,Lim) ->
lists:map(fun (_) -> random:uniform(Lim-StartVal) + StartVal end, lists:seq(1,N)).
You need to correctly seed first of all.
_ = rand:seed(exs1024s),
[rand:uniform(100) || _ <- lists:seq(1, 1000)].
I have a binary M such that 34= will always be present and the rest may vary between any number of digits but will always be an integer.
M = [<<"34=21">>]
When I run this command I get an answer like
hd([X || <<"34=", X/binary >> <- M])
Answer -> <<"21">>
How can I get this to be an integer with the most care taken to make it as efficient as possible?
[<<"34=",X/binary>>] = M,
list_to_integer(binary_to_list(X)).
That yields the integer 21
As of R16B, the BIF binary_to_integer/1 can be used:
OTP-10300
Added four new bifs, erlang:binary_to_integer/1,2,
erlang:integer_to_binary/1, erlang:binary_to_float/1 and
erlang:float_to_binary/1,2. These bifs work similarly to how
their list counterparts work, except they operate on
binaries. In most cases converting from and to binaries is
faster than converting from and to lists.
These bifs are auto-imported into erlang source files and can
therefore be used without the erlang prefix.
So that would look like:
[<<"34=",X/binary>>] = M,
binary_to_integer(X).
A string representation of a number can be converted by N-48. For multi-digit numbers you can fold over the binary, multiplying by the power of the position of the digit:
-spec to_int(binary()) -> integer().
to_int(Bin) when is_binary(Bin) ->
to_int(Bin, {size(Bin), 0}).
to_int(_, {0, Acc}) ->
erlang:trunc(Acc);
to_int(<<N/integer, Tail/binary>>, {Pos, Acc}) when N >= 48, N =< 57 ->
to_int(Tail, {Pos-1, Acc + ((N-48) * math:pow(10, Pos-1))}).
The performance of this is around 100 times slower than using the list_to_integer(binary_to_list(X)) option.