Check if a particular list in a "list of lists" is full or not in Haskell - 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.

Related

Haskell List Comprehension, delete integers from List of numbers

i want to implement a function in list comprehension.
It should deletes the integers in a list of numbers.
And i have a question about it.
delete xs = [ys|ys<-xs, ys /=fromInteger (round ys) ]
xxx.hs> delete [1,2,3.0,4.5,6.7]
[4.5,6.7]
is that means 3.0 is counted as integer instead of float?
And another question:
delete xs = [ys|ys<-xs, ys ==fromInteger (round ys) ]
this time i want it to return integers from a list of numbers.
xxx.hs> delete [1,2,3.0,4.5,6.7]
[1.0,2.0,3.0]
since i did not give the number 1 and 2 in decimal form, why it returns the numbers in decimal?
Thanks for helping me.
I want to implement a function in list comprehension. It should deletes the integers in a list of numbers.
In a list all elements have the same type. So in a list [1.2, 3, 4.5], the 3 is also a value of a type that is a member of the Floating typeclass.
Since i did not give the number 1 and 2 in decimal form, why it returns the numbers in decimal?
Because all the elements are of the same type. GHC will default to Double by the type defaulting rules.
Your filter does not specify that elements should be of an Integral type. This would also be non-sensical since types are resolved at compile time, not at runtime. It simply checks that ys is the same if you fromInteger (round ys). Since round 3.0 == 3, and fromInteger 3 == 3.0 in this case, it thus filters out elements with a fractional part. 1.0 and 2.0 have no fractional part.
The filtering is however not safe, for larger numbers, the mantisse can not represent every integer exactly, and thus this means that some values will be filtered retained when these are not integeral numbers. For more information, see is floating point math broken.

Can a list with repeating values be converted into a multiset in python?

I am having two lists with repeating values and I wanted to take the intersection of the repeating values along with the values that have occurred only once in any one of the lists.
I am just a beginner and would love to hear simple suggestions!
Method 1
l1=[1,2,3,4]
l2=[1,2,5]
intersection=[value for value in l1 if value in l2]
for x in l1+l2:
if x not in intersection:
intersection.append(x)
print(intersection)
Method 2
print(list(set(l1+l2)))

haskell optimized fill of max value

Started building some small real world stuff in Haskell for getting some routine. Need piece of code that takes a value-A (max) and a list of values (x:xs) that will return a list of values of which the sum equals or is lower than value-A. What I have now below, but this keeps fitting numbers until full. It will not return the most optimal sequence.
fill :: Int -> [Int] -> Int
fill max (x:xs) = if x < max then fill (max - x) xs else fill max xs
fill max [] = max
I think I would need to write a function that takes the first value of the list, traverses the rest of the list for the most optimal addition, add that to another list and keep doing that until the smallest remainder or a match with max. Guess might be simple for you but proven a tough nut to crack for me.
Edit: if I sort the value list first, it's a little simpler it seems, since I can discard values that blow up max

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?

First position occurrences in a list of lists

So i have for example this list [[2,1,3],[1,2,3],[2,3,1],[3,1,2]]. What I want from it is to get a list which tells me how many times each of the numbers (in this example just 3) comes in the first position.
length $ filter (\a -> head a == ???) ([[2,1,3],[1,2,3],[2,3,1],[3,1,2]])
The ??? should be 1-2-3, so my result will be [1,2,1] - 1 list has 1 in first position, 2 have the 2 as first position and 1 has 3 as first position.
I am a new to Haskell and I am trying to figure out the proper definition!
Do not try to solve this at once. Start by just creating a list of list beginnings.
So that you get:
[2,1,2,3]
You can then sort, group and count later.

Resources