If, for example, I wanted to define a function that returned true if a=b and b=c, and false if neither one of those equalities were true in Poly ML, how would I write it? I'm not sure how to do more than one conditional simultaneously.
Isn't
a = b andalso b = c
what you want?
I believe this does what you need:
fun f (a, b, c) =
if a = b andalso b = c
then true
else
if a <> b andalso b <> c
then false
else ... (* you haven't specified this case *)
The main points here are:
You can nest conditionals, i.e. have one if expression inside another's then or else case
The operator andalso is the boolean conjunction, meaning x andalso y is true if and only if x evaluates to true and y evaluates to true.
You can put this more concisely using a case expression:
fun f (a, b, c) =
case (a = b, b = c) of
(true, true) => true
| (false, false) => false
| _ => (* you haven't specified this case *)
Related
I'm writing VB Code and I see a question below
There are three positive integers A, B, C
If A is greater than B, C is equal to A+B
If A is less than or equal to B, then C is equal to A-B.
Please use IF...Then and Select/Switch Case to write a program, both of which are used in this program, and additional variables can be added by yourself.
I would like to ask how to write this question, as far as I know, the answer just need only IF Then or Select Case can be realized?
Dim A As Double = 3
Dim B As Double = 2
Dim C As Double = 1
Dim D As Double = 0
D = A - B
Select Case D
Case D > 0
C = A + B
Case D < 0
C = A - B
End Select
If D > 0 Then
C = A + B
ElseIf D < 0 Then
C = A - B
End If
In the real world you wouldn't need to use both an If/Then statement and a Select/Case statement. Since this appears to be homework, I believe the exercise is to see if you can use both conditional check.
I would setup two functions that accept two arguments (a and b) that return the value of c. In your first function use an If/Then and in your second function use a Select/Case.
E.g.
Private Function IfThenOption(a As Integer, b As Integer) As Integer
Dim c As Integer
If (a > b) Then
c = a + b
ElseIf (a < b) Then
c = a - b
Else
c = a
End If
Return c
End Function
Private Function SelectCaseOption(a As Integer, b As Integer) As Integer
Dim c As Integer
Select Case True
Case a > b
c = a + b
Case a < b
c = a - b
Case Else
c = a
End Select
Return c
End Function
Example: https://dotnetfiddle.net/kwcyWc
I have just started reading a book "Type-driven development" and tried my simple example with dependent types. It should return a string for negative numbers and Integer for the positive ones.
I started with 2 holes:
StringOrInt : Bool -> Type
StringOrInt b =
case b of
True => Integer
False => String
getStringOrInt : (x : Integer) -> StringOrInt (x > 0)
getStringOrInt x =
case x > 0 of
True => ?x
False => ?s
If I take a look at holes definition it looks very complicated and not helpful at all:
x : case with block in Prelude.Interfaces.Prelude.Interfaces.Integer implementation of Prelude.Interfaces.Ord, method > (ifThenElse (intToBool (prim__eqBigInt x 0))
(Delay EQ)
(Delay (ifThenElse (intToBool (prim__sltBigInt x
0))
(Delay LT)
(Delay GT))))
x
0 of
True => Integer
False => String
So how to write this function?
Use with rather than case to leverage dependent pattern matching and have the type checker substitute the appropriate Boolean for x > 0 in the result type for each alternative:
StringOrInt : Bool -> Type
StringOrInt True = Integer
StringOrInt False = String
getStringOrInt : (x : Integer) -> StringOrInt (x > 0)
getStringOrInt x with (x > 0)
getStringOrInt x | True = x
getStringOrInt x | False = "<= 0"
I'm trying the hacker rank type of triangle below where based on the 3 sides it has to be determined if the triangle is equilateral, isosceles, scaelene, or not a triangle.
https://www.hackerrank.com/challenges/what-type-of-triangle/problem
I'm not sure why the code below isn't passing the test case. Unfortunately, I can't download the test case to see why it isn't working.
SELECT CASE WHEN A = B AND B = C AND A = C THEN 'Equilateral'
WHEN (A = B AND B != C AND A != C) OR (B = C AND A != B AND A != C) OR (A = C AND A != B AND B != C) THEN 'Isosceles'
WHEN ((A + B) < C) OR ((B + C) < A) OR ((C + A) < B) THEN 'Not a triangle'
ELSE 'Scalene' END
FROM Triangles
Try something like this:
SELECT
CASE
WHEN A + B > C AND A + C > B AND B + C > A THEN
CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
ELSE 'Scalene' END
ELSE 'Not A Triangle' END
FROM TRIANGLES
Only test for the type of triangle when it is a triangle.
Try this :
SELECT CASE WHEN A + B > C AND A+C>B AND B+C>A THEN
CASE WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
WHEN A != B OR B != C OR A != C THEN 'Scalene'
END
ELSE 'Not A Triangle' END FROM TRIANGLES;
The condition for scalene triangle does not include the following condition in your original query : Side 1 + Side 2 <= Side 3
Can I have one question? What is the difference between Condition coverage and Decision coverage?
I have simply example:
IF (A && B) THEN
Condition coverage will have two tests (The result will be false):
A = TRUE, B = FALSE
A = FALSE, B = TRUE
Decision coverage will have only one test (The result will be true):
A = TRUE, B = TRUE
Do I understand that right?
In Condition Coverage (also know as Predicate Coverage) each of the boolean expressions must be evaluated to true and false at least once. For example:
IF ((A || B) && C) THEN
To satisfy the condition coverage criteria, you could use the following tests:
1) A = true | B = not eval | C = false
2) A = false | B = true | C = true
3) A = false | B = false | C = not eval
In Decision Coverage (also know as Branch Coverage) you have to test all posible branches. For example:
...
IF (A){
ELSE IF(B){
}ELSE{
}
...
To satisfy the decision coverage criteria for this piece of code you'll need to run 3 tests:
1) A is evaluated to true
2) A is evaluated to false and B is evaluated to true
3) A and B are evaluated to false
Example:
a = int(input())
if a=0:
print("Zero")
elif a>0:
print("Positive")
else:
print("Negative")
Here the Statement no. 2, 4 and 6 will be considered under Condition Coverage. (Bcz here you are checking the condition that is it equal to 0 or not, is it greater than 0 or not, etc.)
and the statement no. 3, 5 and 7 will be considered under Decision coverage.
(Bcz these are the decision taken after cheking the condition)
Imp Note: Decision Coverage and Branch Coverage are different.
I have some logic and a certain if condition is not being reached. I have tried several variations of if/ifelse/else to create the logic I want, but nothing correctly works. Let me show you in some code...
If(a is true) Then
print("A is true")
Else If(b is true) Then
print("B is true")
if(c is true) Then
print("B and C are true")
else 'c is Not true
print("B is true, C is Not true)
if(d is true) Then
print("B and D are true")
else 'd is Not true
print("B is true, D is Not true")
End If
End If
End If
What is happening is that my
"If(d is true)" and "else 'd is not true"
conditions are not checked. That part of the logic is being "stepped over"
Expected output when A,B,C, and D are true:
"A is true"
Expected output when B, C, and D are true:
"B is true"
"B and C are True"
"B, C, and D are True"
Expected output when B and C are true but D is not:
"B is true"
"B and C are True"
"B is true, D is not true"
Expected outcome when B and D is true:
"B is true"
"B is True, C is not true"
"B, and D are true"
What I am currently seeing:
B,C and D are true:
"B is true"
"B and C are true"
Which leaves out "B and D are true"
Hopefully these outcomes help you understand!
I am not sure what a,b,c or d are but here is what I think you are trying to do. You just need to change the string only if"d" is either true or false based on your "use case". If you want more granularity, then I would suggest a string builder as mentioned before. At any rate, here is the code i used to create your "case" To be clear "c" and "d" are never checked if "b" is false. I based this of what your "use case" stated.
Private Sub Test()
Dim a = True
Dim b = False
Dim c = True
Dim d = True
Dim printout As String = ""
If a Then
printout = "a is true"
Else
If b Then
If c Then
printout = "b and c are true"
else
printout="b is true and c is not true"
End If
If d and c Then
printout = "b and c and d are true"
elseif d=true and c=false then
printout = "b and d are true , c is not true"
elseif c=true and d=false then
printout = "b and c is true ,d is not true"
else
printout = "b is true ,c and d are not true"
End If
End If
End If
Console.WriteLine(printout)
End Sub