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
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 1 year ago.
Improve this question
I'm practicing F# for the first time and I thought I'd try to make a program that would calculate the areas of different kinds of shapes. However, the portion that finds the area of a circle is giving me a lot of trouble.enter code here
elif stringInput = "2" then
let PI = 3.14156
printfn "What is the circle's radius: "
let radiusString = System.Console.ReadLine()
let radiusInt = radiusString |> float
let cirlceArea = (radiusInt * radiusInt) * PI
printfn "The area of the circle is : %d" cirlceArea
I'm sure it has something to do with the radiusString |> float part of the code, but nothing I've tried works and I've had no luck in finding any examples that can help. What can I do?
Ok I just found out the problem was that I was using %d instead of %f
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
if you know, how I can write generator with condition in F# - tell me please!) something like that:
let res = [for i in 1..5 if i % 2 = 0 then i]
printfn "%A" res
You were almost on the right track.
let res =
[
for i in 1..5 do
if i % 2 = 0 then
yield i
]
The feature you're looking for is list comprehensions.
This is similar to yield return in C#. The same comprehensions are available for seq and Array.
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 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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I have a BOOL that has a state of YES or NO, is there a command to flip its state?
Obviously foo=!foo; doesn't apply.
Obviously foo=!foo; doesn't apply.
foo = !foo does apply.
NO example:
BOOL foo = NO;
foo = !foo;
...foo equals YES...
And for any value other than NO:
BOOL foo = YES; // -3 or YES or 100
foo = !foo;
...foo equals NO...
If you find an API which does not return YES or NO, file a bug report. You wouldn't see that from Apple, but some people have taken advantage of the ability to store numbers other than YES or NO in a BOOL (which is a signed char -- it predates C's bool). If needed, you can reduce a BOOL to YES or NO using !!foo, (bool)foo, or bool f = foo;. I favor the last; bool variables.