how do I get past the "parse error on input ‘|’" error? - haskell

I'm new to Haskell and code in general and while trying to write a script that shows which of two numbers is smaller to practise using guards i got stuck with the "parse error on input ‘|’" error.
I read about using "let" somehow but I haven't been able to figure it out. How do I get past this error?
smallest a b = | a >= b = b
| a <= b = a

I'm posting this answer to clarify that you do need the equal sign; but you need one for each case, whereas you essentially duplicated it:
-- +--- this is the equal sign (one per case)
-- |
-- +----------|--- which you are duplicating here
-- | |
-- v v
smallest a b = | a >= b = b
| a <= b = a
that's the reason why the suggested code works.
By the way, the indentation is important, but you can write that snippet on 2 lines rather than 3,
smallest a b | a >= b = b
| otherwise = a
though it does not improve readability, and actually decreseas it if the part before the first | is long.
By the way, as far as I remember, otherwise is truly just a synonym for True (well, the former is a function, and the latter a value constructor, but I guess this makes little difference, as the former is like a 0-arguments function that generates the latter, so they are fundamentally synonyms).

When using guards, you don't need the equals sign. You can simply do this:
smallest a b
| a >= b = b
| otherwise = a
Note the indentation here. Also, since your first condition takes care of both the greater than and equal cases, you can simply use "otherwise" instead of explicitly stating the remaining case.

Related

Haskell: Value Function

I'm still doing the exercise from the book and the one exercise says that I should create a function: value :: Hand -> Int, which returns the value of the hand.
My code so far looks like this:
data Hand = PairOf Rank | ThreeOf1 Rank | ThreeOf2 Suit
value: Hand -> Int
otherwise = 0
--
I now have another problem, because I don't know how to describe "Nothing".
Nothing is described here as a possible hand combination in which Pair, ThreeOf1 and ThreeOf2 do not occur.
Would otherwise = 0 work or doesn't that make much sense?
Thanks again for the suggestions, I corrected them! Thanks also in advance for explanations and help.
otherwise won't work here. What you want is an irrefutable pattern that will match anything, and further since you don't care what gets matched, you specifically want the pattern _:
value :: Hand -> Int
value (PairOf r1) = 1
value (ThreeOf r1) = 2
value (Flush s1) = 3
value _ = 0
Since you don't care about what kind of pair, etc you get, you can also use _ in the other cases:
value :: Hand -> Int
value (PairOf _) = 1
value (ThreeOf _) = 2
value (Flush _) = 3
value _ = 0
If you wanted to match Nothing (or whatever name you come up with that doesn't conflict with the Maybe constructor) specifically, it's just
value Nothing = 0
One of the problems you might be having is that there is no Nothing.
Here's the full type of Hands for holdem:
data HandRank
= HighCard Rank Rank Rank Rank Rank
| OnePair Rank Rank Rank Rank
| TwoPair Rank Rank Rank
| ThreeOfAKind Rank Rank Rank
| Straight Rank
| Flush Rank Rank Rank Rank Rank
| FullHouse Rank Rank
| FourOfAKind Rank Rank
| StraightFlush Rank
deriving (Eq, Ord, Show, Generic)
The extra ranks in the data type are there to distinguish hands, so a pair of aces with a King & Queen kicker beats a pair of aces with a King & Jack kicker. But otherwise, it's the same as your setup.
Once you have a complete transformation from a set of cards to a hand value, there is no nothing. What you might be thinking of as nothing is the HighHand line in the sum type. If your context is that's it for possible hands, then I would add a NoValue to the Hand sum type, as better than outputting a Maybe Hand.
Using the wild cards otherwise _ or value _ introduces a chance of a bug because you might forget about coding up a full house, say, and your existing function would work. If you forget to code one of the sum types, and you don't have a match-any pattern, the compiler will complain. Providing the compiler with all branches of a sum type is also a hot development area of GHC, and it will be fast.

How to implement this functionality in Haskell?

I have defined a data type as follows in Haskell,
data Numbers = "1" | "2" | "3" | "4" | "5"
I want to write a function which will give me the next number if given a number, except if I give it 4(I do not want it to give me 5 then).
next :: Numbers -> Numbers
next number = .....
I am a bit lost on how I can do this. Any suggestions?
You're probably looking for pattern matching syntax.
If you want to go for a more advanced solution, look into deriving Enum and the succ method

Difference between three cells

I have three cells A,B and C (A=80 B=79.1 C=79.1).
I require cell D to display the 0.9 difference that occurs and if the case is none match
`If A=B=C = 0
If A=B<>C = difference between A and C
If A=C<>B = difference between A and B
`
OK. I agree very similar to your other question, but rather than letter references I have taken the differences and wrapped them in ABS as I can't see any sign convention making much sense:
=IF(AND(A1=B1,A1=C1),"match",IF(A1=B1,ABS(A1-C1),IF(A1=C1,ABS(B1-A1),IF(B1=C1,ABS(A1-B1),"none"))))

Why does Stata report zero observations when the variable has non-missing values?

I have a variable called co_dormant that takes on two string values:
Y or N.
So far, when I type summarize co_dormant, I get zero observations.
However, when I type table co_dormant, I get the frequency of Y and N.
I want to keep all observations that have non-missing co_dormant, and when I type
keep if co_dormant != .
all the observations are dropped.
Does anyone know what is happening?
summarize is meant for numeric type variables. (What would be, for example, the mean of a string variable?)
table by default gives the frequency. Stata can count frequencies for either string or numeric type variables.
If you want to drop missings (what Stata considers missings) you can use the missing() function. This works for both string and numeric variables:
clear
set more off
input ///
str1 myvar
Y
N
""
end
list
drop if missing(myvar)
list
See help missing for details on missing values.
If you executed what you say you executed, and the variable was string type, you would get an error:
. input ///
> str1 myvar
myvar
1. Y
2. N
3. ""
4. end
.
. list
+-------+
| myvar |
|-------|
1. | Y |
2. | N |
3. | |
+-------+
.
. keep if myvar != .
type mismatch
r(109);

Algorithm to form a given pattern using some strings

Given are 6 strings of any length. The words are to be arranged in the pattern shown below. They can be arranged either vertically or horizontally.
--------
| |
| |
| |
---------------
| |
| |
| |
--------
The pattern need not to be symmetric and there need to be two empty areas as shown.
For example:
Given strings
PQF
DCC
ACTF
CKTYCA
PGYVQP
DWTP
The pattern can be
DCC...
W.K...
T.T...
PGYVQP
..C..Q
..ACTF
where dot represent empty areas.
The other example is
RVE
LAPAHFUIK
BIRRE
KZGLPFQR
LLHU
UUZZSQHILWB
Pattern is
LLHU....
A..U....
P..Z....
A..Z....
H..S....
F..Q....
U..H....
I..I....
KZGLPFQR
...W...V
...BIRRE
If multiple patterns are possible then pattern with lexicographically smallest first line, then second line and so on is to be formed. What algorithm can be used to solve this?
Find strings which suits to this constraint:
strlen(a) + strlen(b) - 1 = strlen(c)
strlen(d) + strlen(e) - 1 = strlen(f)
After that try every possible situation if they are valid. For example;
aaa.....
d.f.....
d.f.....
d.f.....
cccccccc
..f....e
..f....e
..bbbbbb
There will be 2*2*2 = 8 different situation.
There are a number of heuristics that you can apply, but before that, let's go over some properties of the puzzle.
+aa+
c f
+ee+eee+
f d
+bbb+
Let us call the length of the string with the same character as appeared in the diagram above. We have:
a + b - 1 = e
c + d - 1 = f
I will refer to the 2 strings for the cross in the middle as middle strings.
We also infer that the length of the string cannot be less than 2. Therefore, we can infer:
e > a, e > b
f > c, f > d
From this, we know that the 2 shortest strings cannot be middle strings, due to the inequality above.
The 3 largest strings cannot be equal also, since after choosing any of 3 string as middle string, we are left with 2 largest strings that are equal, and it is impossible according to the inequality above.
The puzzle is only tricky when the lengths are regular. When the lengths are irregular, you can do direct mapping from length to position.
If we have the 2 largest strings being equal, due to the inequality above, they are the 2 middle strings. The worst case for this one is a "regular" puzzle, where the length a, b, c, d are equal.
If the 2 largest strings are unequal, the largest string's position can be determined immediately (since its length is unique in the puzzle) - as one of the middle string. In worst case, there can be 3 candidates for the other middle string - just brute force and check all of them.
Algorithm:
Try to map unique length string to the position.
Brute force the 2 strings in the middle (taken into consideration what I mentioned above), and brute force to fill in the rest.
Even with stupid brute force, there are only 6! = 720 cases, if the string can only go from left to right, up to down (no reverse). There will be 46080 cases (* 2^6) if the string is allowed to be in any direction.

Resources