Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I haven't been doing Erlang for a while so I am practicing but I don't get it anymore :(
-module(conversion).
-export([convert/1, convertMeteoCelcius/1]).
convert({celcius, Degres}) -> {farenheit, (Degres * 1.8) + 32};
convert({celcius, Degres}) -> {celcius, Degres};
convert({farenheit, Degres}) -> {celcius, (Degres - 32)/1.8};
convert({farenheit, Degres}) -> {farenheit, Degres}.
convertMeteoCelcius([], [Result])
-> [Result];
convertMeteoCelcius([{City, {Unit, Temp}}|Rest], [Result])
-> convertMeteoCelcius([Rest], [{City, convert({celcius, Temp})}, Result]).
convertMeteoCelcius([Raw]) -> formatMeteoCelcius([Raw], []).
There is one compiler error: undefined formatMeteoCelcius/2 in the last line. I suppose that you meant convertMeteoCelcius. Changing that, your code compiles.
On the other hand, you then get three warning messages. The third one is about the unused Unit variable, and I suppose you can safely ignore it. The other two, however, show two potential problems in your code:
conversion.erl:5: Warning: this clause cannot match
because a previous clause at line 4 always matches
conversion.erl:7: Warning: this clause cannot match
because a previous clause at line 6 always matches
The first warning basically says that you will have to decide what you want the result of convert({celcius, 0}) to be. It cannot be both {farenheit, 32} and {celcius, 0}.
You may have been mislead by the apparent similarity between Erlang and Prolog. Erlang is not a logic programming language; it is functional. For every function defined using pattern matching, one pattern will be used deterministically every time you call it.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to say if the desired location in the field is 1 return true otherwise return false. Why is this code not working?
fireShot :: Coordinate -> Field -> Bool
fireShot coord Shipfield
| nth ( fst(coord)((nth snd(coord)) ShipField) == 1 = True
| otherwise = False
The brackets in the guard are not balanced, you open five brackets, and you close four brackets. Furthermore variables start with a lowercase, so it should (probably) be shipfield, not Shipfield.
I think it might be better to use pattern matching to obtain the first and second coordinate, since this will make the code more clean. You furthermore do not need guards to return True and False. You can replace the function with:
fireShot :: Coordinate -> Field -> Bool
fireShot (x,y) shipfield = nth x (nth y shipfield) == 1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I've been struggling with the the very first question of the exercise from opendatatructures.org regarding datastructures. I question goes like:
A Dyck word is a sequence of +1's and -1's with the property that the
sum of any prefix of the sequence is never negative. For example,
+1,−1,+1,−1 is a Dyck word, but +1,−1,−1,+1 is not a Dyck word since the prefix +1 − 1 − 1 < 0. Describe any relationship between Dyck
words and Stack push(x) and pop() operations.
How does one find the relation between the operation?
One way to represent check if a word if a Dyck word or not is to use a stack, where you push every time you encounter a +1 and pop every time you encounter a -1. If you ever try to pop from an empty stack, it's not a Dyck word.
Consider the following psuedocode (assume that a word is represented as a array of integers, since the question isn't really about parsing):
boolean isDyck(int[] word) {
Object dummy = new Object(); // Just so you have something to push
Stack stack = new Stack();
for (item : word) {
if (item > 0) {
stack.push(dummy);
} else {
if (stack.isEmpty()) {
return false;
}
stack.pop();
}
}
return true;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Here is my challenge:
Create a function exists that returns true or false when an item is in the list.
1> exists(2,[1,4,5,3,2]).
true
2> exists(2,[]).
false
3> exists(2,[4,5,6,7]).
false
Create a function listLength that will return the length of a list. Using the native function length is not allowed.
1> listLength([1,2,3]).
3
2> listLength([]).
0
AS it looks like homework, I hope you will not get the solution here. I will give you some clues.
For the first one:
there is one case when you know for sure that the term does not exist in the list: when the list is empty.
When the list is not empty the only element of the list you can access is the head of the list, so you will have to use recursion to compare one by one every elements of the list to the input term.
Same thing for the second:
you know the length of an empty list: 0
you know that the length of a not empty list is 1 + length of the tail. Use recursion to count one by one all the elements.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I working on a Swift program and now I have a problem:
How do you compare a Int64 with a Int64?
if(msgCount.value != msg.longLongValue){
Error:
Binary operator '!=' cannot be applied to operands of type 'Int64' and 'Int64'
You can directly compare for equality
Try this, it will help you:
let msgCount : Int64=100
let msg : Int64=101
if(msgCount != msg ){
// perform your logic here.
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am struggling to see if there is an obvious advantage over which method to use when passing values into a function. My code below may not be the best example to explain the decision I'm trying to make, but it is, in my opinion, the easiest to understand.
Variadic Parameter Approach
func arithmeticMean(numbers: Double...) -> Double {
var total: Double = 0
for value in numbers {
total += value
}
return total / Double(numbers.count)
}
arithmeticMean(5, 10, 15)
Array Parameter Approach
func arithmeticMean(numbers: [Double]) -> Double {
var total: Double = 0
for value in numbers {
total += value
}
return total / Double(numbers.count)
}
arithmeticMean([5, 10, 15])
Is either of the two techniques preferred? If so, why (speed, reliability or just ease of reading)? Thanks.
I think there is no speed difference.Because,inside the function,you use Variadic Parameter just as Array.
I think that if the parameters count is small,for example,less than 5,Variadic Parameter may be a better solution,because it is easy to read.
If the count of parameters is large. Array is better solution.
Also know that,Variadic Parameter have some limitation:
A function may have at most one variadic parameter, and it must always appear last in the parameter list, to avoid ambiguity when calling the function with multiple parameters.
If your function has one or more parameters with a default value, and also has a variadic parameter, place the variadic parameter after all the defaulted parameters at the very end of the list.
Just from my idea.Hopes helpful