I need to create a function in sml that takes a single number and returns a list of all the numbers that are prime below it. I can do that but I dont know how to create a list so i can use to see if 1 is prime then 2 then 3 then 4 then 5 and so on.
Basicly i need a way to generate a list inside of an SML function and that list has the numbers from 2 to n.
The List.tabulate function will populate a list for you. Here's an example, giving you the numbers [2..n]:
List.tabulate(n-1, fn x => x+2);
I found out that we can not use external libraries for this so I actually was able to come up with a solution of my own. It does the numbers from start to up to, but not including, ending
fun createList(start:int, ending:int) = if(start = ending) then
[]
else
start::createList(start + 1, ending);
Related
In my code I use Julia's prod() function to do a product over the elements of a list. However, sometimes that list is empty, in which case I want prod(myList) to just return 0 (or any fixed number like 1). I tried searching online, but the only things I could find were for iterators or stuff like that.
I'm using Julia version 1.5.2.
Would a simple ternary operator work for your case?
isempty(my_list) ? 0 : prod(my_list)
What you want is incorrect/unconventional. The product of the elements of an empty sequence should be 1, because that is the multiplicative identity element.
"Any fixed number" is easy:
reduce(*, ls; init=1)
But this does not work well with zero, since that is an annihilator and sends the whole product to zero:
julia> ls = [1,2,3]
3-element Array{Int64,1}:
1
2
3
julia> reduce(*, ls; init=0)
0
Now, returning 1 and then checking for that works if you only have integers. It doesn't as soon as you have a product over rationals, since then the resulting 1 could also stem from x * (1/x).
julia> zeroprod(x) = isempty(x) ? zero(eltype(x)) : prod(x)
zeroprod (generic function with 1 method)
I am currently trying to, inside my function, generate a list. The user will pass in one parameter, which will be an Int. The function's job is to generate a list, starting from 1, and going up to n. So the list would look something like
[1....n]
What I have done thus far is this:
iterate (+1) 1
While this provides the correct pattern, it goes on forever. How would I be able to stop at n? In addition, how would I be able to append '1' at the end of the list, as such:
[1...n,1]
It's literally as simple as:
f :: Int -> [Int]
f n = [1..n] ++ [1]
I have the following code which I have to build upon (i.e. it can't be written a different way). I know there are other better ways of achieving the end result, but I want to use this code and then repeat it to make a list.
from random import choice
number_list = range(1,1001) # Creates a list from 1 to 1000
random_from_list = choice(number_list) # Chooses a random number from the list
I want to now repeat the choice function above 100 times, then print that list of 100 random numbers that I have chosen from my list of 1000 numbers. I have read up on "for" loops but I can't see how to apply it here.
If you don't need to build up your list you could just print them one at a time:
for _ in range(100):
print(choice(number_list))
If you want to build your list first you can use a "list comprehension":
choices = [choice(number_list) for _ in range(100)]
print(choices)
for i in range(100):
print(choice(number_list))
I want to write a program which will read in a list of tuples, and in the tuple it will contain two elements. The first element can be an Object, and the second element will be the quantity of that Object. Just like: Mylist([{Object1,Numbers},{Object2, Numbers}]).
Then I want to read in the Numbers and print the related Object Numbers times and then store them in a list.
So if Mylist([{lol, 3},{lmao, 2}]), then I should get [lol, lol, lol, lmao, lmao] as the final result.
My thought is to first unzip those tuples (imagine if there are more than 2) into two tuples which the first one contains the Objects while the second one contains the quantity numbers.
After that read the numbers in second tuples and then print the related Object in first tuple with the exact times. But I don't know how to do this. THanks for any help!
A list comprehension can do that:
lists:flatten([lists:duplicate(N,A) || {A, N} <- L]).
If you really want printing too, use recursion:
p([]) -> [];
p([{A,N}|T]) ->
FmtString = string:join(lists:duplicate(N,"~p"), " ")++"\n",
D = lists:duplicate(N,A),
io:format(FmtString, D),
D++p(T).
This code creates a format string for io:format/2 using lists:duplicate/2 to replicate the "~p" format specifier N times, joins them with a space with string:join/2, and adds a newline. It then uses lists:duplicate/2 again to get a list of N copies of A, prints those N items using the format string, and then combines the list with the result of a recursive call to create the function result.
I need to write a small Prolog program to count the number of occurrence of each element in a list.
numberOfRepetition(input, result)
For example:
numberOfRepetition([a,b,a,d,c,a,b], X)
can be satisfied with X=[a/3,b/2,d/1,c/1] because a occurs three times, b occurs 2 times and c and d one time.
I don't want to give you the answer, so I gonna help you with it:
% Find the occurrences of given element in list
%
% occurrences([a,b,c,a],a,X).
% -> X = 2.
occurrences([],_,0).
occurrences([X|Y],X,N):- occurrences(Y,X,W),N is W + 1.
occurrences([X|Y],Z,N):- occurrences(Y,Z,N),X\=Z.
Depending on your effort and feedback, I can help you to get your answer.
Check out my answer to the related question "How to count number of element occurrences in a list in Prolog"!
In that answer I present the predicate list_counts/2, which should fot your needs.
Sample use:
:- list_counts([a,b,a,d,c,a,b],Ys).
Ys = [a-3, b-2, d-1, c-1].
Note that that this predicate uses a slightly different representation for key-value pairs expressing multiplicity: principal functor (-)/2 instead of (/)/2.
If possible, switch to the representation using (-)/2 for better interoperability with standard library predicates (like keysort/2).
If you wish to find element with max occurrences:
occurrences([],_,0).
occurrences([X|Y],X,N):- occurrences(Y,X,W),N is W + 1.
occurrences([X|Y],Z,N):- occurrences(Y,Z,N),X\=Z.
**make_list(Max):-
findall((Num,Elem),occurrences([d,d,d,a,a,b,c,d,e],Elem,Num),L),
sort(L,Sorted),
last(Sorted,(_,Max)).**