I need this program in haskel language. already have it in JAVA. Thanks Write th
ID: 3819573 • Letter: I
Question
I need this program in haskel language. already have it in JAVA. Thanks
Write the following. Include a type declaration for each value or function. You should not define a function with one word (because there ha to be an identical function built in) or define a value directly (instead of constructing it with, e.g., a list comprehension) 1. A function "duplicates that takes a list and returns true if any t wo elements in the list are the samc. 2. A list of pairs 'rolls' showing all possible rolls of two six-sided dice. 3. A function "counts that takes a list and returns a list of pairs, with each pair containing an element and the number of times it appears. 4. A list of pairs 'sums', showing how often each sum appears when rolling two six-sided dice.
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Test test = new Test();
//create list having duplicate elements
List<String> list = Arrays.asList(new String[] {"A","B","C","A"});
System.out.println("Check duplicacy in list: ");
System.out.println(test.checkDuplicates(list));
System.out.println("All possible dice rolls are: ");
test.allPossibleDiceRolls("", 2);
System.out.println("Count digit frequency: ");
test.countDigitFrequency(new int[] {1,2,3,1,1,1,2,2,3,4,6,5,5,5});
System.out.println("Number appears in dice");
Map<Integer, Integer> map = test.getDiceMapOfNumberList(new int[] {4,7,8,5});
Set<Integer> keys = map.keySet();
for(int key : keys) {
System.out.println("number " +key+" comes => " + map.get(key) + " times" );
}
}
public <T> boolean checkDuplicates(List<T> list) {
Set<T> set = new HashSet<T>();
for(T data : list)
if(!set.add(data))
return true;
return false;
}
public void allPossibleDiceRolls(String s, int depth) {
if(depth == 0)
System.out.println(s.substring(1));
else
for(int i = 1; i <= 6; i++)
allPossibleDiceRolls(s + "," + i, depth - 1);
}
public void countDigitFrequency(int x[]) {
HashMap<Integer,Integer>digits=new HashMap<Integer,Integer>();
for (int i : x){
if (digits.containsKey(i)){
digits.put(i, digits.get(i)+1);
} else {
digits.put(i, 1);
}
}
for (int key:digits.keySet()){
System.out.println(key+" comes "+digits.get(key) + " times");
}
}
private int numberPossibilitiesInRollingDices(int numberOfDice, int sum) {
if (numberOfDice == sum)
return 1;
else if (numberOfDice == 0 || sum < numberOfDice)
return 0;
else
return numberPossibilitiesInRollingDices(numberOfDice, sum - 1) +
numberPossibilitiesInRollingDices(numberOfDice - 1, sum - 1) -
numberPossibilitiesInRollingDices(numberOfDice - 1, sum - 7);
}
public Map<Integer, Integer> getDiceMapOfNumberList(int[] numbers) {
Map<Integer, Integer> diceMap = new HashMap<Integer, Integer>();
for(int num : numbers) {
diceMap.put(num,numberPossibilitiesInRollingDices(2, num));
}
return diceMap;
}
}
Explanation / Answer
To convert JAVA program into haskel we can use BNF convertor it changes one code to another. It is difficult for us to change the code.Because Haskell is a pure-functional language, loops would be impossible, because you could never increment the loop counter, as there is no (re)assignment.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.