Function with parameters in Haskell - haskell

I'm trying to write a few functions that have parameters in haskell.
For example: I make a list with all kinds of colors, but I want the function to only get the color orange from the list, how do I specify this in the function?
getColor :: a -> a
getColor = orange

You want a function that takes a list of many colours and returns a single colour (presumably of your choosing). You should start with a data type to represent your colours.
data Colour = Red | Orange | Yellow | Green | Blue
now you want a function getColour with the type
getColour :: Colour -> [Colour] -> Colour
which takes a Colour and a list of Colour and picks out the desired colour from the list. However, lists can be empty, or the list might not contain the colour you want! What will getColour return in that case?
In Haskell we handle a function that may not return a result using Maybe. The new type of getColour is
getColour :: Colour -> [Colour] -> Maybe Colour
which means getColour will either return Nothing, or Just colour where colour is from the list.
Lastly, I'll mention that there are a few ways you could actually write the body of getColour, using pattern matching and explicit recursion, or with standard library functions from Haskell's Prelude. I assume you're new to Haskell, so I'd recommend the former. Here's some code to get you started:
getColour _ [] = Nothing
getColour colour (x:xs) = ...
Is this enough to help you write getColour by yourself?

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.

Is there something like DuplicateRecordFields for constructors in Haskell? [duplicate]

i am new to Haskell and probably missing something really basic here, but i am not able to re-use same value constructor among different data types.
data Colour = Red | Pink | Orange | Yellow
data Fruit = Apple | Orange | Banana
This produces error saying
Multiple declarations of ‘Orange’
Not sure why this isn't allowed, i have been using OCaml before learning Haskell and was able to define types like this
As a quick exercise try just defining one of your data types and then opening up GHCi to inspect it.
data Colour = Red | Pink | Orange | Yellow
If you use :t in GHCi, it will tell you the type of anything.
> :t Red
Red :: Colour
> :t Orange
Orange :: Colour
So this tells you that your data constructor Orange is really just a function that takes no arguments and produces a value of type Colour.
So what happens if you add a duplicate declaration?
data Colour = Red | Pink | Orange | Yellow
data Fruit = Apple | Orange | Banana
Now you have defined a function Orange that takes no arguments and produces a value of type Colour or a value of type Fruit. This won't work at all! It would be the same as defining your own custom function foo and giving it multiple type signatures:
foo :: Int
foo :: String
foo = "6"
Which obviously doesn't work either.
To get around this, you can define each data type in its own module, and use a qualified import to scope them correctly:
import qualified Colour as C -- Module Colour.hs
import qualified Fruit as F -- Module Fruit.hs
orange1 = C.Orange :: C.Colour
orange2 = F.Orange :: F.Fruit
Now, you might be thinking "The compiler is smart, it should know what Orange I'm talking about when I'm using it." and you'd be partially right. There is an ongoing effort to bring Overloaded or Duplicate record fields into Haskell. There are various other questions of that ilk already defined here, but I'll list a few
references for further reading.
Why DuplicateRecordFields cannot have type inference?
https://github.com/adamgundry/ghc-proposals/blob/overloaded-record-fields/proposals/0000-overloaded-record-fields.rst
https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/DuplicateRecordFields
There is no particular reason, that is how language was designed. I think the idea was to make sure compiler can infer type for as many expressions as possible. Note that if language will allow to reuse constructors, then you'll have to specify type for show Orange expression - compiler can't infer it anymore. Though now a lot of people don't take this reason seriously, and a lot of modern language extentions do break compiler's ability to infer types for many expressions. So I guess in few years you'll find that your example works already :)

Check if a particular list in a "list of lists" is full or not in Haskell

I'm playing Tic Tac Toe and the columns are represented by lists,
so classic 3x3 Tic Tac full of alternating X and O, from bottom to up, for three columns, would be [X,O,X][X,O,X][X,O,X]. Empty would be represented by Empty I guess (is that a good idea or bad idea)
How would I check if a selected column X, is full?
I want to have a function called Checker :: board -> Int -> Bool
Not really sure where to begin on defining the function Checker.
Edit: Clarifications
1) The board (like any real life game of Tic Tac Toe) will start off obviously as
[empty,empty,empty][empty,empty,empty][empty,empty,empty]
or it will start off as the empty list and a function needs to transform it to
[empty,empty,empty][empty,empty,empty][empty,empty,empty]
2) I want to check if the column is full, so to error check. I do not want players to add X's or O's to full columns. Columns could be filled up with any combination of X's and O's, just like mid way in a real life game of Tic-Tac-Toe.
3) The board is a list of lists. Columns by human interpretation are merely lists
So in a tic tac toe board that is ALL X's EXCEPT the middle being an O, is
[X,X,X][X.O,X][X,X,X]
You can check if a given (1-dimensional) list is all X with
all (== X) list
(As long as your data type has an Eq instance, which you can give it with e.g.
data Square = X | O | Empty
deriving (Eq)
).
Similarly, you can check if every element is non-empty with
all (/= Empty) list
or by defining your own function isFull :: Square -> Bool and using all isFull list.
You can extract a column from a list by mapping a list index operator over it.
column n xss = map (!! n) xss
Another way, which is arguably more elegant, is to transpose it and then look at the rows.

Haskell Test Practice Stuck on Tables and Mapping Functions

This is the question from my exam practice paper:
The following table gives the names, grades and age of people employed by a
company:
Name Grade Age
Able Director 47
Baker Manager 38
Charles Trainee 19
Dunn Director 50
Egglestone Manager 42
i. Define a Haskell type suitable for representing the information in such a
table [10%]
A function avAge is required to find the average age of people in a given grade,
for instance in the example the average age of managers is 40. Give three
alternative Haskell definitions for this function:
ii. using explicit recursion, [20%]
iii. using mapping functions, [20%]
iv. using list comprehensions. [20%]
The table isn't very clear as I couldn't paste the proper table but you can basically see there are 3 columns and multiple rows, one for name, one for grade, one for age. So as you can see the first question "i" is to define a haskell type that is suitable for representing an information in such a table. Keep in my that the real table has lines of course.
So how do I define a function to do this? Does define a function mean e.g. "[String] -> String -> Int" or I have to write up a function that does something?
Finally, about the avAGe to find the average age of people what are the ideas behind doing it with mapping functions? I have planned out for explicit recursion but I'm really struggling to fit mapping functions (map, foldr, filter, etc) to this.
A suitable type would be one where each row has a data type and maybe you can use an existing collection type for holding multiple rows. To start you off:
data Entry = Entry __________ deriving (Eq, Show)
type Entries = __________
So what should go in the blank? It'll need to be able to hold a name, a grade, and an age. For Entries, you should be able to use a built-in type to store all these rows, presumably in order.
Are the grades from a fixed number of valid values? Then you might consider using an ADT to represent them:
data Grade
= Trainee
| Manager
| Director
-- | AnyOtherNameYouNeed
deriving (Eq, Show)
If not, then you can just use Strings, but I would still give them a name:
type Grade = String
So now that you have your types set up, you can work on the implementations of avAge. You need explicit recursion, mapping, and list comprehension. The function needs to take Entries and a Grade and return an average of the ages that match that Grade, so the type signature should probably be
avAgeRec :: Entries -> Grade -> Double
avAgeRec entries grade = __________
avAgeMap :: Entries -> Grade -> Double
avAgeMap entries grade = __________
avAgeComp :: Entries -> Grade -> Double
avAgeComp entries grade = __________
This should help you get started, I just don't want to give you the answers since this is a study problem, and it's always better to come up with the answers yourself =)
So now you have
type Grade = String
type Entry = (String, Grade, Int)
type Entries = [Entry]
And with a little filled in from the comments below:
avAgeRec :: Entries -> Grade -> Double
avAgeRec entries grade = __________
avAgeMap :: Entries -> Grade -> Double
avAgeMap entries grade = <calculate average> $ map <get ages> $ filter <by grade> entries
avAgeComp :: Entries -> Grade -> Double
avAgeComp entries grade = __________
Can you get a few more of the blanks filled in now?

Data storing in haskell - excel like rows and cols

I have a following quest:
I have to write program in Haskell which will allow me to create something like excel sheet.
There are columns and rows, and each cell can hold number or string or some function (sum, mean, multiply etc). Each of the functions take as parameters a list of cells which are summed etc.
Now I am trying to figure out how to store this data into my program...
I was thinking about something like this:
data CellPos = CellPos Int Int -- row and col of Cell
data DataType = Text | String | SumFunction | ...... deriving (Enum)
data Cell = Cell CellPos DataType -- but here is a problem , how to put here data with type which depends on DataType???
I wanted just to have big list of Cell and search in it for specified column/row etc
But there must be some better solution for this – maybe some two dimensional array which auto adjust its size or something?
I will have to save/load a sheet to /from file...
Let's answer one question at a time:
data Cell = Cell CellPos DataType
"but here is a problem , how to put here data with type which depends on DataType???"
Put that data into DataType:
data DataType = Text String | Number Double | Function CellPos (DataType -> DataType)
"I wanted just to have big list of Cell and search in it for specified column/row etc. But there must be some better solution for this - maybe some two dimmensional array which auto adjust its size or something?"
I suggest a Map CellPos DataType.
"I will have to save/load a sheet to /from file..."
The simplest thing will probably be to derive Show and Read and use the resulting functions together with readFile and writeFile. The only caveat here (with respect to DataType as defined earlier in this answer) is that functions cannot be serialized. To get around this, make a more explicit type for the functions in cells -- perhaps an abstract syntax tree for some simple expression language.

Resources