N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and Scala - Part 1

Philip Schwarz
Philip SchwarzSoftware development is what I am passionate about
N-Queens Combinatorial Problem
first see the problem solved using the List monad and a Scala for comprehension
then see the Scala program translated into Haskell, both using a do expressions and using a List comprehension
understand how the Scala for comprehension is desugared, and what role the withFilter function plays
also understand how the Haskell do expressions and List comprehension are desugared, and what role the guard function plays
Miran Lipovača
Polyglot FP for Fun and Profit – Haskell and Scala
@philip_schwarz
slides by https://www.slideshare.net/pjschwarz
Bill Venners
Martin Odersky
Part 1
based on, and inspired by, the work of
Lex Spoon
Yes, I know, Lex Spoon looks a bit too young in that photo, and Bill Venner’s hair doesn’t look realistic in that drawing. Martin
Odersky’s drawing is not bad at all.
By the way, don’t be put off by the fact that the book image below is of the fourth edition. Excerpts from the N-queens section of
that edition are great, and by the way, in the fifth edition (new for Scala 3) I don’t see that section (it may have moved to upcoming
book Advanced Programming in Scala). Also, I do use Scala 3 in this slide deck.
Bill Venners
Martin Odersky
Lex Spoon
@philip_schwarz
A particularly suitable application area of for expressions are
combinatorial puzzles.
An example of such a puzzle is the 8-queens problem: Given a
standard chess-board, place eight queens such that no queen is
in check from any other (a queen can check another piece if they
are on the same column, row, or diagonal).
To find a solution to this problem, it’s actually simpler to generalize it to chess-
boards of arbitrary size.
Hence the problem is to place N queens on a chess-board of N x N squares, where
the size N is arbitrary.
We’ll start numbering cells at one, so the upper-left cell of an N x N board has
coordinate (1,1) and the lower-right cell has coordinate (N,N).
To solve the N-queens problem, note that you need to place a queen in each row.
So you could place queens in successive rows, each time checking that a newly
placed queen is not in check from any other queens that have already been placed.
In the course of this search, it might happen that a queen that needs to be placed in
row k would be in check in all fields of that row from queens in row 1 to k - 1. In
that case, you need to abort that part of the search in order to continue with a
different configuration of queens in columns 1 to k - 1.
Here on the right is a sample
solution to the puzzle.
And below is an example in which there is
nowhere to place queen number 6
because every other cell on the board is in
check from queens 1 to 5.
An imperative solution to this problem would place queens one by one, moving
them around on the board.
But it looks difficult to come up with a scheme that really tries all possibilities.
A more functional approach represents a solution directly, as a value. A solution
consists of a list of coordinates, one for each queen placed on the board.
Note, however, that a full solution can not be found in a single step. It needs to be
built up gradually, by occupying successive rows with queens.
This suggests a recursive algorithm.
Assume you have already generated all solutions of placing k queens on a board
of size N x N, where k is less than N.
Each solution can be represented by a list of length k of coordinates (row,
column), where both row and column numbers range from 1 to N.
It’s convenient to treat these partial solution lists as stacks, where the
coordinates of the queen in row k come first in the list, followed by the
coordinates of the queen in row k - 1, and so on.
The bottom of the stack is the coordinate of the queen placed in the first row of
the board.
All solutions together are represented as a list of lists, with one element for each
solution.
List((8, 6), (7, 3),
(6, 7), (5, 2),
(4, 8), (3, 5),
(2, 1), (1, 4))
See below for the list representing the
above solution to the 8-queens puzzle.
(7, 3)
(8, 6)
(6, 7)
(5, 2)
(4, 8)
(3, 5)
(2, 1)
(1, 4)
R C
O O
W L
Now, to place the next queen in row k + 1, generate all possible extensions of each previous solution
by one more queen.
This yields another list of solutions lists, this time of length k + 1.
Continue the process until you have obtained all solutions of the size of the chess-board N.
This algorithmic idea is embodied in function placeQueens below:
The outer function queens in the program above simply calls placeQueens with the size of the board n
as its argument.
The task of the function application placeQueens(k) is to generate all partial solutions of length k in a
list.
Every element of the list is one solution, represented by a list of length k. So placeQueens returns a list
of lists.
def queens(n: Int): List[List[(Int,Int)]] = {
def placeQueens(k: Int): List[List[(Int,Int)]] =
if (k == 0)
List(List())
else
for {
queens <- placeQueens(k - 1)
column <- 1 to n
queen = (k, column)
if isSafe(queen, queens)
} yield queen :: queens
placeQueens(n)
}
List((8, 6), (7, 3),
(6, 7), (5, 2),
(4, 8), (3, 5),
(2, 1), (1, 4))
If the parameter k to placeQueens is 0, this means that it needs to generate all solutions of
placing zero queens on zero rows. There is only one solution: place no queen at all. This
solution is represented by the empty list. So if k is zero, placeQueens returns List(List()), a
list consisting of a single element that is the empty list.
Note that this is quite different from the empty list List(). If placeQueens returns List(), this
means no solutions, instead of a single solution consisting of no placed queens.
In the other case, where k is not zero, all the work of placeQueens is done in the for
expression.
The first generator of that for expression iterates through all solutions of placing k - 1 queens on
the board.
The second generator iterates through all possible columns on which the kth queen might be
placed.
The third part of the for expression defines the newly considered queen position to be the pair
consisting of row k and each produced column.
The fourth part of the for expression is a filter which checks with isSafe whether the new
queen is safe from check by all previous queens (the definition of isSafe will be discussed a bit
later.)
If the new queen is not in check from any other queens, it can form part of a partial solution, so
placeQueens generates with queen::queens a new solution.
If the new queen is not safe from check, the filter returns false, so no solution is generated.
for {
queens <- placeQueens(k - 1)
column <- 1 to n
queen = (k, column)
if isSafe(queen, queens)
} yield queen :: queens
Here is the for expression
again, for reference.
The only remaining bit is the isSafe method, which is used to check whether a given queen is in check from any other element
in a list of queens.
Here is the definition:
The isSafe method expresses that a queen is safe with respect to some other queens if it is not in check from any other queen.
The inCheck method expresses that queens q1 and q2 are mutually in check.
It returns true in one of three cases:
1. If the two queens have the same row coordinate.
2. If the two queens have the same column coordinate.
3. If the two queens are on the same diagonal (i.e., the difference between their rows and the difference between their
columns are the same).
The first case – that the two queens have the same row coordinate – cannot happen in the application because placeQueens
already takes care to place each queen in a different row. So you could remove the test without changing the functionality of the
program.
def isSafe(queen: (Int, Int), queens: List[(Int, Int)]) =
queens forall (q => !inCheck(queen, q))
def inCheck(q1: (Int, Int), q2: (Int, Int)) =
q1._1 == q2._1 || // same row
q1._2 == q2._2 || // same column
(q1._1 - q2._1).abs == (q1._2 - q2._2).abs // on diagonal
(6, 2)
(7, 5)
(5, 6)
(4, 1)
(3, 7)
(2, 4)
(1, 0)
(0, 3) In online course FP in Scala, the board
coordinates are zero-based, and since
it is obvious that the nth queen is in row
n, a solution is a list of column indices
rather than a list of coordinate pairs.
List(5, 2, 6, 1, 7, 4, 0, 3) @philip_schwarz
Because a column index is sufficient to indicate the coordinates of the nth queen, we can see that the type of isSafe’s first parameter is now Int, i.e. a
column index, rather than (Int, Int) i.e. a coordinate pair.
Also, the type of a solution is List[Int] rather than List[(Int, Int)], and solutions are returned in a Set, rather than a List, to distinguish between a
single solution, which is an ordered list, and the collection of all solutions, which is not ordered (though it could be, if we so wanted).
def queens(n: Int): List[List[(Int,Int)]] = {
def placeQueens(k: Int): List[List[(Int,Int)]] =
if (k == 0)
List(List())
else
for {
queens <- placeQueens(k - 1)
column <- 1 to n
queen = (k, column)
if isSafe(queen, queens)
} yield queen :: queens
placeQueens(n)
}
def queens(n: Int): Set[List[Int]] = {
def placeQueens(k: Int): Set[List[Int]] =
if (k == 0)
Set(List())
else
for {
queens <- placeQueens(k - 1)
col <- 0 until n
if isSafe(col, queens)
} yield col :: queens
placeQueens(n)
}
def isSafe(queen: (Int, Int), queens: List[(Int, Int)]) =
queens forall (q => !inCheck(queen, q))
def inCheck(q1: (Int, Int), q2: (Int, Int)) =
q1._1 == q2._1 || // same row
q1._2 == q2._2 || // same column
(q1._1 - q2._1).abs == (q1._2 - q2._2).abs // on diagonal
def isSafe(col: Int, queens: List[Int]): Boolean = {
val row = queens.length
val queensWithRow = (row - 1 to 0 by -1) zip queens
queensWithRow forall
{ case (r, c) => col != c && math.abs(col - c) != row - r }
}
Here are the two versions of
the program side by side.
def queens(n: Int): List[List[(Int,Int)]] =
def placeQueens(k: Int): List[List[(Int,Int)]] =
if k == 0
then List(List())
else
for
queens <- placeQueens(k - 1)
column <- 1 to n
queen = (k, column)
if isSafe(queen, queens)
yield queen :: queens
placeQueens(n)
def isSafe(queen: (Int, Int), queens: List[(Int, Int)]) =
queens forall (q => !inCheck(queen, q))
def inCheck(queen1: (Int, Int), queen2: (Int, Int)) =
queen1(0) == queen2(0) || // same row
queen1(1) == queen2(1) || // same column
(queen1(0) - queen2(0)).abs == (queen1(1) - queen2(1)).abs // on diagonal
def queens(n: Int): Set[List[Int]] =
def placeQueens(k: Int): Set[List[Int]] =
if k == 0
then Set(List())
else
for
queens <- placeQueens(k - 1)
col <- 0 until n
if isSafe(col, queens)
yield col :: queens
placeQueens(n)
def isSafe(col: Int, queens: List[Int]): Boolean =
val row = queens.length
val queensWithRow = (row - 1 to 0 by -1) zip queens
queensWithRow forall
{ case (r, c) => col != c && math.abs(col - c) != row - r }
Same as on the previous slide, but with
really minor tweaks thanks to Scala 3
def queens(n: Int): List[List[Int]] =
def placeQueens(k: Int): List[List[Int]] =
if k == 0
then List(List())
else
for
queens <- placeQueens(k - 1)
queen <- 1 to n
if safe(queen, queens)
yield queen :: queens
placeQueens(n)
def onDiagonal(row: Int, column: Int, otherRow: Int, otherColumn: Int) =
math.abs(row - otherRow) == math.abs(column - otherColumn)
def zipWithRows(queens: List[Int]): Iterable[(Int,Int)] =
val rowCount = queens.length
val rowNumbers = rowCount - 1 to 0 by -1
rowNumbers zip queens
def safe(queen: Int, queens: List[Int]): Boolean =
val (row, column) = (queens.length, queen)
val safe: ((Int,Int)) => Boolean = (nextRow, nextColumn) =>
column != nextColumn && !onDiagonal(column, row, nextColumn, nextRow)
zipWithRows(queens) forall safe
In the next part of this slide deck, we are going to
be using the program on the right.
The queens and placeQueens functions are pretty
much the same as in the second version on the
previous slide, but board coordinates are one-
based, and the two functions return a List rather
than a Set.
The logic for determining if a queen is safe from
previously placed queens, is organised in a way that
is in part a hybrid between the two programs on
the previous slide.
(7, 3)
(8, 6)
(6, 7)
(5, 2)
(4, 8)
(3, 5)
(2, 1)
(1, 4)
List(6, 3, 7, 2, 8, 5, 1, 4)
assert(queens(4) == List(List(3, 1, 4, 2), List(2, 4, 1, 3)))
assert(queens(6) == List(List(3, 6, 2, 5, 1, 4),
List(4, 1, 5, 2, 6, 3),
List(5, 3, 1, 6, 4, 2),
List(2, 4, 6, 1, 3, 5)))
@philip_schwarz
The first thing we are going to do, is translate our N-queens
program from Scala into Haskell.
Let’s start with the queens and placeQueens functions.
queens n = placeQueens n
where
placeQueens 0 = [[]]
placeQueens k =
do
queens <- placeQueens(k-1)
queen <- [1..n]
guard (safe queen queens)
return (queen:queens)
def queens(n: Int): List[List[Int]] =
def placeQueens(k: Int): List[List[Int]] =
if k == 0
then List(List())
else
for
queens <- placeQueens(k - 1)
queen <- 1 to n
if safe(queen, queens)
yield queen :: queens
placeQueens(n)
In Scala we use a for comprehension,
whereas in Haskell we use a do expression.
In Scala, the filtering is done by
if safe(queen, queens)
whereas in Haskell it is done by
guard (safe queen queens)
If you are asking yourself what the guard function
is, don’t worry: we’ll be looking at it soon.
queens n = placeQueens n
where
placeQueens 0 = [[]]
placeQueens k = do
queens <- placeQueens(k-1)
queen <- [1..n]
guard (safe queen queens)
return (queen:queens)
queens n = placeQueens n
where
placeQueens 0 = [[]]
placeQueens k =
[queen:queens |
queens <- placeQueens(k-1),
queen <- [1..n],
safe queen queens]
We can simplify the Haskell version a little bit by eliminating
the calls to guard and return, which can be achieved by
switching from a do expression to a list comprehension.
If you are asking yourself how the list
comprehension eliminates the need for guard and
return, don’t worry: we’ll be looking at that soon.
queens n = placeQueens n
where
placeQueens 0 = [[]]
placeQueens k = [queen:queens |
queens <- placeQueens(k-1),
queen <- [1..n],
safe queen queens]
safe queen queens = all safe (zipWithRows queens)
where
safe (r,c) = c /= col && not (onDiagonal col row c r)
row = length queens
col = queen
onDiagonal row column otherRow otherColumn =
abs (row - otherRow) == abs (column - otherColumn)
zipWithRows queens = zip rowNumbers queens
where
rowCount = length queens
rowNumbers = [rowCount-1,rowCount-2..0]
def queens(n: Int): List[List[Int]] =
def placeQueens(k: Int): List[List[Int]] =
if k == 0
then List(List())
else
for
queens <- placeQueens(k - 1)
queen <- 1 to n
if safe(queen, queens)
yield queen :: queens
placeQueens(n)
def onDiagonal(row: Int, column: Int, otherRow: Int, otherColumn: Int) =
math.abs(row - otherRow) == math.abs(column - otherColumn)
def safe(queen: Int, queens: List[Int]): Boolean =
val (row, column) = (queens.length, queen)
val safe: ((Int,Int)) => Boolean = (nextRow, nextColumn) =>
column != nextColumn && !onDiagonal(column, row, nextColumn, nextRow)
zipWithRows(queens) forall safe
def zipWithRows(queens: List[Int]): Iterable[(Int,Int)] =
val rowCount = queens.length
val rowNumbers = rowCount - 1 to 0 by -1
rowNumbers zip queens
And here is the translation
of the whole program
haskell> queens 4
[[3,1,4,2],[2,4,1,3]]
haskell> queens 5
[[4,2,5,3,1],[3,5,2,4,1],[5,3,1,4,2],[4,1,3,5,2],
[5,2,4,1,3],[1,4,2,5,3],[2,5,3,1,4],[1,3,5,2,4],
[3,1,4,2,5],[2,4,1,3,5]]
haskell> queens 6
[[5,3,1,6,4,2],[4,1,5,2,6,3],
[3,6,2,5,1,4],[2,4,6,1,3,5]]
scala> queens(4)
val res0: List[List[Int]] = List(List(3, 1, 4, 2), List(2, 4, 1, 3))
scala> queens(5)
val res1: List[List[Int]] = List(List(4, 2, 5, 3, 1), List(3, 5, 2, 4, 1), List(5, 3, 1, 4, 2), List(4, 1, 3, 5, 2),
List(5, 2, 4, 1, 3), List(1, 4, 2, 5, 3), List(2, 5, 3, 1, 4), List(1, 3, 5, 2, 4),
List(3, 1, 4, 2, 5), List(2, 4, 1, 3, 5))
scala> queens(6)
val res2: List[List[Int]] = List(List(5, 3, 1, 6, 4, 2), List(4, 1, 5, 2, 6, 3),
List(3, 6, 2, 5, 1, 4), List(2, 4, 6, 1, 3, 5))
Let’s try out both the Scala
program and the Haskell one.
@philip_schwarz
N N-queens solution count
1 1
2 0
3 0
4 2
5 10
6 4
7 40
8 92
9 352
10 724
11 2,680
12 14,200
13 73,712
14 365,596
15 2,279,184
16 14,772,512
17 95,815,104
18 666,090,624
19 4,968,057,848
20 39,029,188,884
21 314,666,222,712
22 2,691,008,701,644
23 24,233,937,684,440
24 227,514,171,973,736
25 2,207,893,435,808,352
26 22,317,699,616,364,044
27 234,907,967,154,122,528
data source: https://en.wikipedia.org/wiki/Eight_queens_puzzle
scala> queens(8).size
val res0: Int = 92
scala> queens(9).size
val res1: Int = 352
scala> queens(10).size
val res2: Int = 724
scala> queens(11).size
val res3: Int = 2680
scala> queens(12).size
val res4: Int = 14200
scala> queens(13).size
val res5: Int = 73712
scala> queens(14).size
val res6: Int = 365596
Haskell> length (queens 8)
92
Haskell> length (queens 9)
352
Haskell> length (queens 10)
724
Haskell> length (queens 11)
2680
Haskell> length (queens 12)
14200
Haskell> length (queens 13)
73712
Haskell> length (queens 14)
365596
For N > 6, the number of solutions is too large for the solutions to
be displayed simply by printing them to the screen.
Let’s at least check that we get the expected number of solutions.
In the next five slides, we are going to look at how the Scala
for comprehension is implemented under the hood.
@philip_schwarz
for
queens <- placeQueens(k - 1)
queen <- 1 to n
yield queen :: queens
placeQueens(k - 1) flatMap { queens =>
(1 to n) map { queen =>
queen :: queens
}
}
for
nums <- List(List(1,2),List(3,4))
num <- List(10,20)
yield num :: nums
List(List(10, 1, 2), List(20, 1, 2), List(10, 3, 4), List(20, 3, 4))
List(List(1,2),List(3,4)) flatMap { nums =>
List(10,20) map { num =>
num :: nums
}
}
Here is how the N-queens for comprehension, without
the filter, is implemented, i.e. what it looks like when it
is desugared (when its syntactic sugar is removed).
To aid our understanding, here is a similar for
comprehension that is simpler to understand, but can
be run at the REPL, together with its desugared version.
And here is the value produced by both the for
comprehension and its desugared equivalent.
for
queens <- placeQueens(k - 1)
queen <- 1 to n
if safe(queen, queens)
yield queen :: queens
placeQueens(k - 1) flatMap { queens =>
(1 to n) withFilter { queen =>
safe(queen, queens)
} map { queen =>
queen :: queens
}
}
Same as on the previous slide, except that now we are adding a filter to the for comprehension.
When the filter is desugared, it results in a call to List’s withFilter function.
for
nums <- List(List(1,2),List(3,4))
num <- List(10,20)
if (num > 15)
yield num :: nums
List(List(1,2),List(3,4)) flatMap { nums =>
List(10,20) withFilter { num =>
num > 15
} map { num =>
num :: nums
}
}
To aid our understanding, here is a similar for
comprehension that is simpler to understand, and that can
be run at the REPL, together with its desugared version.
List(List(20, 1, 2), List(20, 3, 4))
And here is the value produced by both the for comprehension and its desugared equivalent.
Let’s see what the withFilter function does:
• it is applied to a monad, in this case a List
• it takes a predicate, i.e. a function that takes a parameter and returns either true or false.
• it applies the predicate to each value yielded by the monad (in this case, each value contained in a List)
• it filters out the values for which the predicate returns false
• It lets through the values for which the predicate returns true
On the next slide, we look at an example of how the behaviour of a for comprehension (or its desugared equivalent) is
affected by the behaviour of the withFilter function.
@philip_schwarz
for
nums <- List(List(1,2),List(3,4))
num <- List(10,20)
if (num > 15)
yield num :: nums
List(List(1,2),List(3,4)) flatMap { nums =>
List(10,20) withFilter { num =>
num > 15
} map { num =>
num :: nums
}
}
List(List(20, 1, 2), List(20, 3, 4))
for
nums <- List(List(1,2),List(3,4))
num <- List(10,20)
if (true)
yield num :: nums
List(List(1,2),List(3,4)) flatMap { nums =>
List(10,20) withFilter { num =>
true
} map { num =>
num :: nums
}
}
List(List(10, 1, 2), List(20, 1, 2), List(10, 3, 4), List(20, 3, 4))
for
nums <- List(List(1,2),List(3,4))
num <- List(10,20)
if (false)
yield num :: nums
List(List(1,2),List(3,4)) flatMap { nums =>
List(10,20) withFilter { num =>
false
} map { num =>
num :: nums
}
}
List()
Notice that the predicate function passed to withFilter may or may not
use its parameter. Like the map and flatMap functions, withFilter is
used to bind the value(s) yielded by a monad (if any) to a variable name
which is then available for use in subsequent computations.
withFilter
lets through
some elements
withFilter
lets through
all elements
withFilter
lets through
no elements
The difference is that while the functions passed to map
and flatMap are almost certain to use the variable, in the
case of withFilter, it may choose to influence the
overall computation without actually using the variable.
scala> List(1,2,3).withFilter(n => true).map(identity)
val res0: List[Int] = List(1, 2, 3)
scala> List(1,2,3).withFilter(n => n > 1).map(identity)
val res1: List[Int] = List(2, 3)
scala> List(1,2,3).withFilter(n => false).map(identity)
val res2: List[Int] = List()
Before we move on, here is a final, illustration
of what the withFilter function does.
Earlier we saw that the do expression that is the Haskell translation of the Scala for
comprehension, involves the use of the guard function.
Also, later we intend to look at how the Haskell do expression is implemented under the hood,
which involves both the guard function and the (>>) function.
So, in the next three slides, we are going to look at how Miran Lipovača explains guard and (>>).
Here is the default implementation of the (>>) function:
(>>) :: (Monad m) => ma -> mb -> mb
m >> n = m >>= _ -> n
Normally, passing some value to a function that ignores its parameter and always
returns some predetermined value always results in that predetermined value. With
monads however, their context and meaning must be considered as well.
Here is how >> acts with Maybe:
ghci> Nothing >> Just 3
Nothing
ghci> Just 3 >> Just 4
Just 4
ghci> Just 3 >> Nothing
Nothing
Miran Lipovača
ghci> [] >> [3]
[]
ghci> [3] >> [4]
[4]
ghci> [3] >> []
[]
Here on the
right is how >>
acts with lists.
>>= is Haskell’s equivalent of Scala’s
flatMap and _ -> n is Haskell’s
equivalent of Scala’s anonymous
function _ => n.
scala> List() >> List(3)
val res0: List[Int] = List()
scala> List(3) >> List(4)
val res1: List[Int] = List(4)
scala> List(3) >> List()
val res2: List[Nothing] = List()
scala> List() >> List()
val res3: List[Nothing] = List()
scala> None >> 3.some
val res0: Option[Int] = None
scala> 3.some >> 4.some
val res1: Option[Int] = Some(4)
scala> 3.some >> None
val res2: Option[Nothing] = None
scala> None >> None
val res3: Option[Nothing] = None
Same as above, but using
the Scala Cats library:
import cats._
import cats.implicits._
MonadPlus and the guard Function
List comprehensions allow us to filter our output. For instance, we can filter a list of numbers to search only for numbers
whose digit contains a 7:
ghci> [x | x <- [1..50], '7' `elem` show x]
[7,17,27,37,47]
We apply show to x to turn our number into a string, and then we check if the character ‘7’ is part of that string. To see how
filtering in list comprehensions translates to the list monad, we need to check out the guard function and the MonadPlus type
class. The MonadPlus type class is for monads that can also act as monoids. Here is its definition:
class Monad m => MonadPlus m where
mzero :: ma
mplus :: ma -> ma -> ma
mzero is synonymous with mempty from the Monoid type class, and mplus corresponds to mappend. Because lists are monoids
as well as monads, they can be made an instance of this type class:
instance MonadPlus [] where
mzero = []
mplus = (++)
For lists, mzero represents a nondeterministic computation that has no results at all – a failed computation. mplus joins two
nondeterministic values into one. The guard function is defined like this:
guard :: Bool -> m ()
guard True = return ()
guard False = mzero
guard takes a Boolean value. If that value is True, guard takes a () and puts it in a minimal default context that succeeds. If
the Boolean value is False, guard makes a failed monadic value.
Miran Lipovača
Here it is in action:
ghci> guard (5 > 2) :: Maybe ()
Just ()
ghci> guard (1 > 2) :: Maybe ()
Nothing
ghci> guard (5 > 2) :: [()]
[()]
ghci> guard (1 > 2) :: [()]
[]
This looks interesting, but how is it useful? In the list monad, we use it to filter out nondeterministic computations:
ghci> [1..50] >>= (x -> guard ('7' `elem` show x) >> return x)
[7,17,27,37,47]
The result here is the same as the result of our previous list comprehension. How does guard achieve this? Let’s first see
how guard functions in conjunction with >>:
ghci> guard (5 > 2) >> return "cool" :: [String]
["cool"]
ghci> guard (1 > 2) >> return "cool" :: [String]
[]
If guard succeeds, the result contained within it is the empty tuple. So then we use >> to ignore the empty tuple and present
something else as the result. However, if guard fails, then so will the return later on, because feeding an empty list to a
function with >>= always results in an empty list. guard basically says, “If this Boolean is False, then produce a failure right
here. Otherwise, make a successful value that has a dummy result of () inside it.” All this does is to allow the computation to
continue.
Miran Lipovača
ghci> [x | x <- [1..50], '7' `elem` show x]
[7,17,27,37,47]
same result
for
queens <- placeQueens(k - 1)
queen <- 1 to n
if safe(queen, queens)
yield queen :: queens
placeQueens(k - 1) flatMap { queens =>
(1 to n) withFilter { queen =>
safe(queen, queens)
} map { queen =>
queen :: queens
}
}
do
queens <- placeQueens(k-1)
queen <- [1..n]
guard (safe queen queens)
return (queen:queens)
placeQueens(k-1) >>= queens ->
[1..n] >>= queen ->
guard (safe queen queens) >>
return (queen:queens)
Here we see the desugaring of the Scala for comprehension
again, but we also desugar the equivalent Haskell do expression.
Note how, while the desugared Scala code introduces the use of flatMap, withFilter and map, the desugared Haskell
code introduces the use of >>= (Haskell’s equivalent of flatMap) and >>.
Note also how, in Haskell, the guard function is used in both the do expression and in its desugared equivalent.
for
nums <- List(List(1,2),List(3,4))
num <- List(10,20)
if (num > 15)
yield num :: nums
List(List(1,2),List(3,4)) flatMap { nums =>
List(10,20) withFilter { num =>
num > 15
} map { num =>
num :: nums
}
}
do
nums <- [[1,2],[3,4]]
num <- [10,20]
guard (num > 15)
return (num:nums)
[[1,2],[3,4]] >>= nums ->
[10,20] >>= num ->
guard (num > 15) >>
return (num:nums)
[[20,1,2],[20,3,4]]
List(List(20, 1, 2), List(20, 3, 4))
Same as on the previous slide, except that here the logic
is simpler to understand and can be run at the REPL.
@philip_schwarz
do
nums <- [[1,2],[3,4]]
num <- [10,20]
guard (num > 15)
return (num:nums)
[[1,2],[3,4]] >>= nums ->
[10,20] >>= num ->
guard (num > 15) >>
return (num:nums)
List(List(20, 1, 2), List(20, 3, 4))
do
nums <- [[1,2],[3,4]]
num <- [10,20]
guard (True)
return (num:nums)
[[1,2],[3,4]] >>= nums ->
[10,20] >>= num ->
guard (True) >>
return (num:nums)
List(List(10, 1, 2), List(20, 1, 2), List(10, 3, 4), List(20, 3, 4))
do
nums <- [[1,2],[3,4]]
num <- [10,20]
guard (False)
return (num:nums)
[[1,2],[3,4]] >>= nums ->
[10,20] >>= num ->
guard (False) >>
return (num:nums)
List()
guard lets
through some
elements
guard lets
through all
elements
guard lets
through no
elements
ghci> [1,2] >>= n -> ['a',’b’] >>= ch -> return (n,ch)
[(1,'a'),(1,'b'),(2,'a'),(2,'b’)]
…
…
Here is the previous expression rewritten in do notation:
listOfTuples :: [(Int,Char)]
listOfTuples do
n <- [1,2]
ch <- [’a’,’b’]
return (n,ch)
Do Notation and List Comprehensions
Using lists with do notation might remind you of something you’ve seen before. For instance, check out the following piece
of code:
ghci> [(n, ch) | n <- [1,2], ch <- ['a','b’]]
[(1,'a'),(1,'b'),(2,'a'),(2,'b’)]
Yes, list comprehensions! In our do notation example, n became every result from [1,2]. For every such result, ch was
assigned a result from ['a','b’], and then the final line put (n,ch) into a default context (a singleton list) to present it as the
result without introducing any additional nondeterminism.
In this list comprehension, the same thing happened, but we didn’t need to write return at the end to present (n,ch) as the
result, because the output part of a list comprehension did that for us.
In fact, list comprehensions are just syntactic sugar for using lists as monads. In the end, list comprehensions and lists in
do notation translate to using >>= to do computations that feature nondeterminism. Miran Lipovača
do queens <- placeQueens(k-1)
queen <- [1..n]
guard (safe queen queens)
return (queen:queens)
[queen:queens |
queens <- placeQueens(k-1),
queen <- [1..n],
safe queen queens]
placeQueens(k-1) >>= queens ->
[1..n] >>= queen ->
guard (safe queen queens) >>
return (queen:queens)
We saw earlier what the Haskell do expression looks like when it is desugared.
Desugaring the list comprehension produces the same result.
queens n = placeQueens n
where
placeQueens 0 = [[]]
placeQueens k = [queen:queens |
queens <- placeQueens(k-1),
queen <- [1..n],
safe queen queens]
safe queen queens = all safe (zipWithRows queens)
where
safe (r,c) = c /= col && not (onDiagonal col row c r)
row = length queens
col = queen
onDiagonal row column otherRow otherColumn =
abs (row - otherRow) == abs (column - otherColumn)
zipWithRows queens = zip rowNumbers queens
where
rowCount = length queens
rowNumbers = [rowCount-1,rowCount-2..0]
def queens(n: Int): List[List[Int]] =
def placeQueens(k: Int): List[List[Int]] =
if k == 0
then List(List())
else
for
queens <- placeQueens(k - 1)
queen <- 1 to n
if safe(queen, queens)
yield queen :: queens
placeQueens(n)
def onDiagonal(row: Int, column: Int, otherRow: Int, otherColumn: Int) =
math.abs(row - otherRow) == math.abs(column - otherColumn)
def safe(queen: Int, queens: List[Int]): Boolean =
val (row, column) = (queens.length, queen)
val safe: ((Int,Int)) => Boolean = (nextRow, nextColumn) =>
column != nextColumn && !onDiagonal(column, row, nextColumn, nextRow)
zipWithRows(queens) forall safe
def zipWithRows(queens: List[Int]): Iterable[(Int,Int)] =
val rowCount = queens.length
val rowNumbers = rowCount - 1 to 0 by -1
rowNumbers zip queens
As a recap, let’s see again the translation of
the whole program from Scala to Haskell.
There is lots to do in part two. We’ll kick off by taking
the Scala version of the program, and extending it so
that it can display a single solution board as follows:
To conclude the first part of this slide deck,
see below for a simple way of displaying
the solutions to the N-queens problem.
We’ll then get the program to display,
all together, the results of queens(N)
for N = 4, 5, 6, 7, 8. Plus much more.
See you then.
@philip_schwarz
1 of 34

Recommended

Left and Right Folds - Comparison of a mathematical definition and a programm... by
Left and Right Folds- Comparison of a mathematical definition and a programm...Left and Right Folds- Comparison of a mathematical definition and a programm...
Left and Right Folds - Comparison of a mathematical definition and a programm...Philip Schwarz
985 views14 slides
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ... by
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Philip Schwarz
187 views58 slides
Computer Graphics in Java and Scala - Part 1 by
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Philip Schwarz
403 views25 slides
Functions by
FunctionsFunctions
FunctionsGenny Phillips
3.1K views7 slides
Graph a function by
Graph a functionGraph a function
Graph a functionSanaullahMemon10
184 views43 slides
The Exponential and natural log functions by
The Exponential and natural log functionsThe Exponential and natural log functions
The Exponential and natural log functionsJJkedst
4.5K views46 slides

More Related Content

What's hot

Integration material by
Integration material Integration material
Integration material Surya Swaroop
45 views72 slides
Types of functions 05272011 by
Types of functions 05272011Types of functions 05272011
Types of functions 05272011Boyet Aluan
1.2K views21 slides
4.1 inverse functions by
4.1 inverse functions4.1 inverse functions
4.1 inverse functionsmath260
1.3K views97 slides
Functions and graphs by
Functions and graphsFunctions and graphs
Functions and graphsSujata Tapare
2.7K views28 slides
Functions and its Applications in Mathematics by
Functions and its Applications in MathematicsFunctions and its Applications in Mathematics
Functions and its Applications in MathematicsAmit Amola
15.3K views19 slides
Lec3 by
Lec3Lec3
Lec3Anjneya Varshney
883 views33 slides

What's hot(20)

Types of functions 05272011 by Boyet Aluan
Types of functions 05272011Types of functions 05272011
Types of functions 05272011
Boyet Aluan1.2K views
4.1 inverse functions by math260
4.1 inverse functions4.1 inverse functions
4.1 inverse functions
math2601.3K views
Functions and its Applications in Mathematics by Amit Amola
Functions and its Applications in MathematicsFunctions and its Applications in Mathematics
Functions and its Applications in Mathematics
Amit Amola15.3K views
Addendum to ‘Monads do not Compose’ by Philip Schwarz
Addendum to ‘Monads do not Compose’ Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’
Philip Schwarz501 views
L2 graphs piecewise, absolute,and greatest integer by James Tagara
L2 graphs  piecewise, absolute,and greatest integerL2 graphs  piecewise, absolute,and greatest integer
L2 graphs piecewise, absolute,and greatest integer
James Tagara2.5K views
Module1 exponential functions by dionesioable
Module1  exponential functionsModule1  exponential functions
Module1 exponential functions
dionesioable10.4K views
Lesson 1: Functions and their Representations by Matthew Leingang
Lesson 1: Functions and their RepresentationsLesson 1: Functions and their Representations
Lesson 1: Functions and their Representations
Matthew Leingang8.2K views
Math 4 lecture on Graphing Rational Functions by Leo Crisologo
Math 4 lecture on Graphing Rational FunctionsMath 4 lecture on Graphing Rational Functions
Math 4 lecture on Graphing Rational Functions
Leo Crisologo1.8K views
Computer Graphics in Java and Scala - Part 1b by Philip Schwarz
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1b
Philip Schwarz261 views
2 integration and the substitution methods x by math266
2 integration and the substitution methods x2 integration and the substitution methods x
2 integration and the substitution methods x
math266599 views
5.2 the substitution methods by math265
5.2 the substitution methods5.2 the substitution methods
5.2 the substitution methods
math2652.4K views
Bostock and Chandler chapter3 functions by Sarah Sue Calbio
Bostock and Chandler chapter3 functionsBostock and Chandler chapter3 functions
Bostock and Chandler chapter3 functions
Sarah Sue Calbio2.8K views
2.9 graphs of factorable polynomials by math260
2.9 graphs of factorable polynomials2.9 graphs of factorable polynomials
2.9 graphs of factorable polynomials
math2602.1K views

Similar to N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and Scala - Part 1

Chap04alg by
Chap04algChap04alg
Chap04algMunhchimeg
470 views22 slides
Chap04alg by
Chap04algChap04alg
Chap04algMunkhchimeg
560 views22 slides
Algorithm Performance For Chessboard Separation Problems by
Algorithm Performance For Chessboard Separation ProblemsAlgorithm Performance For Chessboard Separation Problems
Algorithm Performance For Chessboard Separation ProblemsTina Gabel
2 views16 slides
backtracking 8 Queen.pptx by
backtracking 8 Queen.pptxbacktracking 8 Queen.pptx
backtracking 8 Queen.pptxJoshipavanEdduluru1
3 views20 slides
Unit 4 jwfiles by
Unit 4 jwfilesUnit 4 jwfiles
Unit 4 jwfilesNv Thejaswini
728 views16 slides
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle by
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cyclevarun arora
3.2K views30 slides

Similar to N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and Scala - Part 1(20)

Algorithm Performance For Chessboard Separation Problems by Tina Gabel
Algorithm Performance For Chessboard Separation ProblemsAlgorithm Performance For Chessboard Separation Problems
Algorithm Performance For Chessboard Separation Problems
Tina Gabel2 views
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle by varun arora
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
varun arora3.2K views
Codeware by Uri Nativ
CodewareCodeware
Codeware
Uri Nativ66.9K views
N QUEENS PROBLEM USING BACKTRACKING.pptx by 42bvaishnaviraut
N QUEENS PROBLEM USING BACKTRACKING.pptxN QUEENS PROBLEM USING BACKTRACKING.pptx
N QUEENS PROBLEM USING BACKTRACKING.pptx
42bvaishnaviraut51 views
N QUEENS PROBLEM USING BACKTRACKING.pptx by Kalyani Raut
N QUEENS PROBLEM USING BACKTRACKING.pptxN QUEENS PROBLEM USING BACKTRACKING.pptx
N QUEENS PROBLEM USING BACKTRACKING.pptx
Kalyani Raut39 views
Backtracking by Raghu nath
BacktrackingBacktracking
Backtracking
Raghu nath310 views
MC0082 –Theory of Computer Science by Aravind NC
MC0082 –Theory of Computer ScienceMC0082 –Theory of Computer Science
MC0082 –Theory of Computer Science
Aravind NC1.4K views
Skiena algorithm 2007 lecture20 satisfiability by zukun
Skiena algorithm 2007 lecture20 satisfiabilitySkiena algorithm 2007 lecture20 satisfiability
Skiena algorithm 2007 lecture20 satisfiability
zukun440 views
01 knapsack using backtracking by mandlapure
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
mandlapure52.9K views

More from Philip Schwarz

Scala Left Fold Parallelisation - Three Approaches by
Scala Left Fold Parallelisation- Three ApproachesScala Left Fold Parallelisation- Three Approaches
Scala Left Fold Parallelisation - Three ApproachesPhilip Schwarz
41 views44 slides
Tagless Final Encoding - Algebras and Interpreters and also Programs by
Tagless Final Encoding - Algebras and Interpreters and also ProgramsTagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also ProgramsPhilip Schwarz
45 views16 slides
Fusing Transformations of Strict Scala Collections with Views by
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsPhilip Schwarz
20 views28 slides
A sighting of sequence function in Practical FP in Scala by
A sighting of sequence function in Practical FP in ScalaA sighting of sequence function in Practical FP in Scala
A sighting of sequence function in Practical FP in ScalaPhilip Schwarz
28 views4 slides
N-Queens Combinatorial Puzzle meets Cats by
N-Queens Combinatorial Puzzle meets CatsN-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets CatsPhilip Schwarz
33 views386 slides
Kleisli composition, flatMap, join, map, unit - implementation and interrelat... by
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Philip Schwarz
12 views16 slides

More from Philip Schwarz(20)

Scala Left Fold Parallelisation - Three Approaches by Philip Schwarz
Scala Left Fold Parallelisation- Three ApproachesScala Left Fold Parallelisation- Three Approaches
Scala Left Fold Parallelisation - Three Approaches
Philip Schwarz41 views
Tagless Final Encoding - Algebras and Interpreters and also Programs by Philip Schwarz
Tagless Final Encoding - Algebras and Interpreters and also ProgramsTagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also Programs
Philip Schwarz45 views
Fusing Transformations of Strict Scala Collections with Views by Philip Schwarz
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with Views
Philip Schwarz20 views
A sighting of sequence function in Practical FP in Scala by Philip Schwarz
A sighting of sequence function in Practical FP in ScalaA sighting of sequence function in Practical FP in Scala
A sighting of sequence function in Practical FP in Scala
Philip Schwarz28 views
N-Queens Combinatorial Puzzle meets Cats by Philip Schwarz
N-Queens Combinatorial Puzzle meets CatsN-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets Cats
Philip Schwarz33 views
Kleisli composition, flatMap, join, map, unit - implementation and interrelat... by Philip Schwarz
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Philip Schwarz12 views
The aggregate function - from sequential and parallel folds to parallel aggre... by Philip Schwarz
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
Philip Schwarz263 views
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske... by Philip Schwarz
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
Philip Schwarz250 views
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha... by Philip Schwarz
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Philip Schwarz827 views
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t... by Philip Schwarz
Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Philip Schwarz1.2K views
Jordan Peterson - The pursuit of meaning and related ethical axioms by Philip Schwarz
Jordan Peterson - The pursuit of meaning and related ethical axiomsJordan Peterson - The pursuit of meaning and related ethical axioms
Jordan Peterson - The pursuit of meaning and related ethical axioms
Philip Schwarz141 views
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c... by Philip Schwarz
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Philip Schwarz100 views
Defining filter using (a) recursion (b) folding with S, B and I combinators (... by Philip Schwarz
Defining filter using (a) recursion (b) folding with S, B and I combinators (...Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Philip Schwarz78 views
The Sieve of Eratosthenes - Part 1 - with minor corrections by Philip Schwarz
The Sieve of Eratosthenes - Part 1 - with minor correctionsThe Sieve of Eratosthenes - Part 1 - with minor corrections
The Sieve of Eratosthenes - Part 1 - with minor corrections
Philip Schwarz110 views
The Sieve of Eratosthenes - Part 1 by Philip Schwarz
The Sieve of Eratosthenes - Part 1The Sieve of Eratosthenes - Part 1
The Sieve of Eratosthenes - Part 1
Philip Schwarz1K views
The Uniform Access Principle by Philip Schwarz
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
Philip Schwarz542 views
The Expression Problem - Part 2 by Philip Schwarz
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2
Philip Schwarz650 views
The Expression Problem - Part 1 by Philip Schwarz
The Expression Problem - Part 1The Expression Problem - Part 1
The Expression Problem - Part 1
Philip Schwarz1.2K views
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac... by Philip Schwarz
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Philip Schwarz268 views
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ... by Philip Schwarz
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Philip Schwarz312 views

Recently uploaded

Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... by
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Donato Onofri
825 views34 slides
HarshithAkkapelli_Presentation.pdf by
HarshithAkkapelli_Presentation.pdfHarshithAkkapelli_Presentation.pdf
HarshithAkkapelli_Presentation.pdfharshithakkapelli
11 views16 slides
Programming Field by
Programming FieldProgramming Field
Programming Fieldthehardtechnology
5 views9 slides
The Era of Large Language Models.pptx by
The Era of Large Language Models.pptxThe Era of Large Language Models.pptx
The Era of Large Language Models.pptxAbdulVahedShaik
5 views9 slides
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -... by
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...Deltares
6 views15 slides
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema by
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - GeertsemaDSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - GeertsemaDeltares
17 views13 slides

Recently uploaded(20)

Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... by Donato Onofri
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Donato Onofri825 views
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -... by Deltares
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
Deltares6 views
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - GeertsemaDSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
Deltares17 views
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... by Lisi Hocke
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Lisi Hocke30 views
360 graden fabriek by info33492
360 graden fabriek360 graden fabriek
360 graden fabriek
info3349238 views
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx by animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm14 views
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... by Deltares
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
Deltares9 views
Fleet Management Software in India by Fleetable
Fleet Management Software in India Fleet Management Software in India
Fleet Management Software in India
Fleetable11 views
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut... by Deltares
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...
Deltares7 views
FIMA 2023 Neo4j & FS - Entity Resolution.pptx by Neo4j
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptx
Neo4j7 views
Software evolution understanding: Automatic extraction of software identifier... by Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ... by Deltares
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
Deltares11 views
Quality Engineer: A Day in the Life by John Valentino
Quality Engineer: A Day in the LifeQuality Engineer: A Day in the Life
Quality Engineer: A Day in the Life
John Valentino6 views
Dapr Unleashed: Accelerating Microservice Development by Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
Miroslav Janeski10 views
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko... by Deltares
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
Deltares14 views

N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and Scala - Part 1

  • 1. N-Queens Combinatorial Problem first see the problem solved using the List monad and a Scala for comprehension then see the Scala program translated into Haskell, both using a do expressions and using a List comprehension understand how the Scala for comprehension is desugared, and what role the withFilter function plays also understand how the Haskell do expressions and List comprehension are desugared, and what role the guard function plays Miran Lipovača Polyglot FP for Fun and Profit – Haskell and Scala @philip_schwarz slides by https://www.slideshare.net/pjschwarz Bill Venners Martin Odersky Part 1 based on, and inspired by, the work of Lex Spoon
  • 2. Yes, I know, Lex Spoon looks a bit too young in that photo, and Bill Venner’s hair doesn’t look realistic in that drawing. Martin Odersky’s drawing is not bad at all. By the way, don’t be put off by the fact that the book image below is of the fourth edition. Excerpts from the N-queens section of that edition are great, and by the way, in the fifth edition (new for Scala 3) I don’t see that section (it may have moved to upcoming book Advanced Programming in Scala). Also, I do use Scala 3 in this slide deck. Bill Venners Martin Odersky Lex Spoon @philip_schwarz
  • 3. A particularly suitable application area of for expressions are combinatorial puzzles. An example of such a puzzle is the 8-queens problem: Given a standard chess-board, place eight queens such that no queen is in check from any other (a queen can check another piece if they are on the same column, row, or diagonal). To find a solution to this problem, it’s actually simpler to generalize it to chess- boards of arbitrary size. Hence the problem is to place N queens on a chess-board of N x N squares, where the size N is arbitrary. We’ll start numbering cells at one, so the upper-left cell of an N x N board has coordinate (1,1) and the lower-right cell has coordinate (N,N). To solve the N-queens problem, note that you need to place a queen in each row. So you could place queens in successive rows, each time checking that a newly placed queen is not in check from any other queens that have already been placed. In the course of this search, it might happen that a queen that needs to be placed in row k would be in check in all fields of that row from queens in row 1 to k - 1. In that case, you need to abort that part of the search in order to continue with a different configuration of queens in columns 1 to k - 1. Here on the right is a sample solution to the puzzle. And below is an example in which there is nowhere to place queen number 6 because every other cell on the board is in check from queens 1 to 5.
  • 4. An imperative solution to this problem would place queens one by one, moving them around on the board. But it looks difficult to come up with a scheme that really tries all possibilities. A more functional approach represents a solution directly, as a value. A solution consists of a list of coordinates, one for each queen placed on the board. Note, however, that a full solution can not be found in a single step. It needs to be built up gradually, by occupying successive rows with queens. This suggests a recursive algorithm. Assume you have already generated all solutions of placing k queens on a board of size N x N, where k is less than N. Each solution can be represented by a list of length k of coordinates (row, column), where both row and column numbers range from 1 to N. It’s convenient to treat these partial solution lists as stacks, where the coordinates of the queen in row k come first in the list, followed by the coordinates of the queen in row k - 1, and so on. The bottom of the stack is the coordinate of the queen placed in the first row of the board. All solutions together are represented as a list of lists, with one element for each solution. List((8, 6), (7, 3), (6, 7), (5, 2), (4, 8), (3, 5), (2, 1), (1, 4)) See below for the list representing the above solution to the 8-queens puzzle. (7, 3) (8, 6) (6, 7) (5, 2) (4, 8) (3, 5) (2, 1) (1, 4) R C O O W L
  • 5. Now, to place the next queen in row k + 1, generate all possible extensions of each previous solution by one more queen. This yields another list of solutions lists, this time of length k + 1. Continue the process until you have obtained all solutions of the size of the chess-board N. This algorithmic idea is embodied in function placeQueens below: The outer function queens in the program above simply calls placeQueens with the size of the board n as its argument. The task of the function application placeQueens(k) is to generate all partial solutions of length k in a list. Every element of the list is one solution, represented by a list of length k. So placeQueens returns a list of lists. def queens(n: Int): List[List[(Int,Int)]] = { def placeQueens(k: Int): List[List[(Int,Int)]] = if (k == 0) List(List()) else for { queens <- placeQueens(k - 1) column <- 1 to n queen = (k, column) if isSafe(queen, queens) } yield queen :: queens placeQueens(n) } List((8, 6), (7, 3), (6, 7), (5, 2), (4, 8), (3, 5), (2, 1), (1, 4))
  • 6. If the parameter k to placeQueens is 0, this means that it needs to generate all solutions of placing zero queens on zero rows. There is only one solution: place no queen at all. This solution is represented by the empty list. So if k is zero, placeQueens returns List(List()), a list consisting of a single element that is the empty list. Note that this is quite different from the empty list List(). If placeQueens returns List(), this means no solutions, instead of a single solution consisting of no placed queens. In the other case, where k is not zero, all the work of placeQueens is done in the for expression. The first generator of that for expression iterates through all solutions of placing k - 1 queens on the board. The second generator iterates through all possible columns on which the kth queen might be placed. The third part of the for expression defines the newly considered queen position to be the pair consisting of row k and each produced column. The fourth part of the for expression is a filter which checks with isSafe whether the new queen is safe from check by all previous queens (the definition of isSafe will be discussed a bit later.) If the new queen is not in check from any other queens, it can form part of a partial solution, so placeQueens generates with queen::queens a new solution. If the new queen is not safe from check, the filter returns false, so no solution is generated. for { queens <- placeQueens(k - 1) column <- 1 to n queen = (k, column) if isSafe(queen, queens) } yield queen :: queens Here is the for expression again, for reference.
  • 7. The only remaining bit is the isSafe method, which is used to check whether a given queen is in check from any other element in a list of queens. Here is the definition: The isSafe method expresses that a queen is safe with respect to some other queens if it is not in check from any other queen. The inCheck method expresses that queens q1 and q2 are mutually in check. It returns true in one of three cases: 1. If the two queens have the same row coordinate. 2. If the two queens have the same column coordinate. 3. If the two queens are on the same diagonal (i.e., the difference between their rows and the difference between their columns are the same). The first case – that the two queens have the same row coordinate – cannot happen in the application because placeQueens already takes care to place each queen in a different row. So you could remove the test without changing the functionality of the program. def isSafe(queen: (Int, Int), queens: List[(Int, Int)]) = queens forall (q => !inCheck(queen, q)) def inCheck(q1: (Int, Int), q2: (Int, Int)) = q1._1 == q2._1 || // same row q1._2 == q2._2 || // same column (q1._1 - q2._1).abs == (q1._2 - q2._2).abs // on diagonal
  • 8. (6, 2) (7, 5) (5, 6) (4, 1) (3, 7) (2, 4) (1, 0) (0, 3) In online course FP in Scala, the board coordinates are zero-based, and since it is obvious that the nth queen is in row n, a solution is a list of column indices rather than a list of coordinate pairs. List(5, 2, 6, 1, 7, 4, 0, 3) @philip_schwarz
  • 9. Because a column index is sufficient to indicate the coordinates of the nth queen, we can see that the type of isSafe’s first parameter is now Int, i.e. a column index, rather than (Int, Int) i.e. a coordinate pair. Also, the type of a solution is List[Int] rather than List[(Int, Int)], and solutions are returned in a Set, rather than a List, to distinguish between a single solution, which is an ordered list, and the collection of all solutions, which is not ordered (though it could be, if we so wanted).
  • 10. def queens(n: Int): List[List[(Int,Int)]] = { def placeQueens(k: Int): List[List[(Int,Int)]] = if (k == 0) List(List()) else for { queens <- placeQueens(k - 1) column <- 1 to n queen = (k, column) if isSafe(queen, queens) } yield queen :: queens placeQueens(n) } def queens(n: Int): Set[List[Int]] = { def placeQueens(k: Int): Set[List[Int]] = if (k == 0) Set(List()) else for { queens <- placeQueens(k - 1) col <- 0 until n if isSafe(col, queens) } yield col :: queens placeQueens(n) } def isSafe(queen: (Int, Int), queens: List[(Int, Int)]) = queens forall (q => !inCheck(queen, q)) def inCheck(q1: (Int, Int), q2: (Int, Int)) = q1._1 == q2._1 || // same row q1._2 == q2._2 || // same column (q1._1 - q2._1).abs == (q1._2 - q2._2).abs // on diagonal def isSafe(col: Int, queens: List[Int]): Boolean = { val row = queens.length val queensWithRow = (row - 1 to 0 by -1) zip queens queensWithRow forall { case (r, c) => col != c && math.abs(col - c) != row - r } } Here are the two versions of the program side by side.
  • 11. def queens(n: Int): List[List[(Int,Int)]] = def placeQueens(k: Int): List[List[(Int,Int)]] = if k == 0 then List(List()) else for queens <- placeQueens(k - 1) column <- 1 to n queen = (k, column) if isSafe(queen, queens) yield queen :: queens placeQueens(n) def isSafe(queen: (Int, Int), queens: List[(Int, Int)]) = queens forall (q => !inCheck(queen, q)) def inCheck(queen1: (Int, Int), queen2: (Int, Int)) = queen1(0) == queen2(0) || // same row queen1(1) == queen2(1) || // same column (queen1(0) - queen2(0)).abs == (queen1(1) - queen2(1)).abs // on diagonal def queens(n: Int): Set[List[Int]] = def placeQueens(k: Int): Set[List[Int]] = if k == 0 then Set(List()) else for queens <- placeQueens(k - 1) col <- 0 until n if isSafe(col, queens) yield col :: queens placeQueens(n) def isSafe(col: Int, queens: List[Int]): Boolean = val row = queens.length val queensWithRow = (row - 1 to 0 by -1) zip queens queensWithRow forall { case (r, c) => col != c && math.abs(col - c) != row - r } Same as on the previous slide, but with really minor tweaks thanks to Scala 3
  • 12. def queens(n: Int): List[List[Int]] = def placeQueens(k: Int): List[List[Int]] = if k == 0 then List(List()) else for queens <- placeQueens(k - 1) queen <- 1 to n if safe(queen, queens) yield queen :: queens placeQueens(n) def onDiagonal(row: Int, column: Int, otherRow: Int, otherColumn: Int) = math.abs(row - otherRow) == math.abs(column - otherColumn) def zipWithRows(queens: List[Int]): Iterable[(Int,Int)] = val rowCount = queens.length val rowNumbers = rowCount - 1 to 0 by -1 rowNumbers zip queens def safe(queen: Int, queens: List[Int]): Boolean = val (row, column) = (queens.length, queen) val safe: ((Int,Int)) => Boolean = (nextRow, nextColumn) => column != nextColumn && !onDiagonal(column, row, nextColumn, nextRow) zipWithRows(queens) forall safe In the next part of this slide deck, we are going to be using the program on the right. The queens and placeQueens functions are pretty much the same as in the second version on the previous slide, but board coordinates are one- based, and the two functions return a List rather than a Set. The logic for determining if a queen is safe from previously placed queens, is organised in a way that is in part a hybrid between the two programs on the previous slide. (7, 3) (8, 6) (6, 7) (5, 2) (4, 8) (3, 5) (2, 1) (1, 4) List(6, 3, 7, 2, 8, 5, 1, 4) assert(queens(4) == List(List(3, 1, 4, 2), List(2, 4, 1, 3))) assert(queens(6) == List(List(3, 6, 2, 5, 1, 4), List(4, 1, 5, 2, 6, 3), List(5, 3, 1, 6, 4, 2), List(2, 4, 6, 1, 3, 5))) @philip_schwarz
  • 13. The first thing we are going to do, is translate our N-queens program from Scala into Haskell. Let’s start with the queens and placeQueens functions. queens n = placeQueens n where placeQueens 0 = [[]] placeQueens k = do queens <- placeQueens(k-1) queen <- [1..n] guard (safe queen queens) return (queen:queens) def queens(n: Int): List[List[Int]] = def placeQueens(k: Int): List[List[Int]] = if k == 0 then List(List()) else for queens <- placeQueens(k - 1) queen <- 1 to n if safe(queen, queens) yield queen :: queens placeQueens(n) In Scala we use a for comprehension, whereas in Haskell we use a do expression. In Scala, the filtering is done by if safe(queen, queens) whereas in Haskell it is done by guard (safe queen queens) If you are asking yourself what the guard function is, don’t worry: we’ll be looking at it soon.
  • 14. queens n = placeQueens n where placeQueens 0 = [[]] placeQueens k = do queens <- placeQueens(k-1) queen <- [1..n] guard (safe queen queens) return (queen:queens) queens n = placeQueens n where placeQueens 0 = [[]] placeQueens k = [queen:queens | queens <- placeQueens(k-1), queen <- [1..n], safe queen queens] We can simplify the Haskell version a little bit by eliminating the calls to guard and return, which can be achieved by switching from a do expression to a list comprehension. If you are asking yourself how the list comprehension eliminates the need for guard and return, don’t worry: we’ll be looking at that soon.
  • 15. queens n = placeQueens n where placeQueens 0 = [[]] placeQueens k = [queen:queens | queens <- placeQueens(k-1), queen <- [1..n], safe queen queens] safe queen queens = all safe (zipWithRows queens) where safe (r,c) = c /= col && not (onDiagonal col row c r) row = length queens col = queen onDiagonal row column otherRow otherColumn = abs (row - otherRow) == abs (column - otherColumn) zipWithRows queens = zip rowNumbers queens where rowCount = length queens rowNumbers = [rowCount-1,rowCount-2..0] def queens(n: Int): List[List[Int]] = def placeQueens(k: Int): List[List[Int]] = if k == 0 then List(List()) else for queens <- placeQueens(k - 1) queen <- 1 to n if safe(queen, queens) yield queen :: queens placeQueens(n) def onDiagonal(row: Int, column: Int, otherRow: Int, otherColumn: Int) = math.abs(row - otherRow) == math.abs(column - otherColumn) def safe(queen: Int, queens: List[Int]): Boolean = val (row, column) = (queens.length, queen) val safe: ((Int,Int)) => Boolean = (nextRow, nextColumn) => column != nextColumn && !onDiagonal(column, row, nextColumn, nextRow) zipWithRows(queens) forall safe def zipWithRows(queens: List[Int]): Iterable[(Int,Int)] = val rowCount = queens.length val rowNumbers = rowCount - 1 to 0 by -1 rowNumbers zip queens And here is the translation of the whole program
  • 16. haskell> queens 4 [[3,1,4,2],[2,4,1,3]] haskell> queens 5 [[4,2,5,3,1],[3,5,2,4,1],[5,3,1,4,2],[4,1,3,5,2], [5,2,4,1,3],[1,4,2,5,3],[2,5,3,1,4],[1,3,5,2,4], [3,1,4,2,5],[2,4,1,3,5]] haskell> queens 6 [[5,3,1,6,4,2],[4,1,5,2,6,3], [3,6,2,5,1,4],[2,4,6,1,3,5]] scala> queens(4) val res0: List[List[Int]] = List(List(3, 1, 4, 2), List(2, 4, 1, 3)) scala> queens(5) val res1: List[List[Int]] = List(List(4, 2, 5, 3, 1), List(3, 5, 2, 4, 1), List(5, 3, 1, 4, 2), List(4, 1, 3, 5, 2), List(5, 2, 4, 1, 3), List(1, 4, 2, 5, 3), List(2, 5, 3, 1, 4), List(1, 3, 5, 2, 4), List(3, 1, 4, 2, 5), List(2, 4, 1, 3, 5)) scala> queens(6) val res2: List[List[Int]] = List(List(5, 3, 1, 6, 4, 2), List(4, 1, 5, 2, 6, 3), List(3, 6, 2, 5, 1, 4), List(2, 4, 6, 1, 3, 5)) Let’s try out both the Scala program and the Haskell one. @philip_schwarz
  • 17. N N-queens solution count 1 1 2 0 3 0 4 2 5 10 6 4 7 40 8 92 9 352 10 724 11 2,680 12 14,200 13 73,712 14 365,596 15 2,279,184 16 14,772,512 17 95,815,104 18 666,090,624 19 4,968,057,848 20 39,029,188,884 21 314,666,222,712 22 2,691,008,701,644 23 24,233,937,684,440 24 227,514,171,973,736 25 2,207,893,435,808,352 26 22,317,699,616,364,044 27 234,907,967,154,122,528 data source: https://en.wikipedia.org/wiki/Eight_queens_puzzle scala> queens(8).size val res0: Int = 92 scala> queens(9).size val res1: Int = 352 scala> queens(10).size val res2: Int = 724 scala> queens(11).size val res3: Int = 2680 scala> queens(12).size val res4: Int = 14200 scala> queens(13).size val res5: Int = 73712 scala> queens(14).size val res6: Int = 365596 Haskell> length (queens 8) 92 Haskell> length (queens 9) 352 Haskell> length (queens 10) 724 Haskell> length (queens 11) 2680 Haskell> length (queens 12) 14200 Haskell> length (queens 13) 73712 Haskell> length (queens 14) 365596 For N > 6, the number of solutions is too large for the solutions to be displayed simply by printing them to the screen. Let’s at least check that we get the expected number of solutions.
  • 18. In the next five slides, we are going to look at how the Scala for comprehension is implemented under the hood. @philip_schwarz
  • 19. for queens <- placeQueens(k - 1) queen <- 1 to n yield queen :: queens placeQueens(k - 1) flatMap { queens => (1 to n) map { queen => queen :: queens } } for nums <- List(List(1,2),List(3,4)) num <- List(10,20) yield num :: nums List(List(10, 1, 2), List(20, 1, 2), List(10, 3, 4), List(20, 3, 4)) List(List(1,2),List(3,4)) flatMap { nums => List(10,20) map { num => num :: nums } } Here is how the N-queens for comprehension, without the filter, is implemented, i.e. what it looks like when it is desugared (when its syntactic sugar is removed). To aid our understanding, here is a similar for comprehension that is simpler to understand, but can be run at the REPL, together with its desugared version. And here is the value produced by both the for comprehension and its desugared equivalent.
  • 20. for queens <- placeQueens(k - 1) queen <- 1 to n if safe(queen, queens) yield queen :: queens placeQueens(k - 1) flatMap { queens => (1 to n) withFilter { queen => safe(queen, queens) } map { queen => queen :: queens } } Same as on the previous slide, except that now we are adding a filter to the for comprehension. When the filter is desugared, it results in a call to List’s withFilter function. for nums <- List(List(1,2),List(3,4)) num <- List(10,20) if (num > 15) yield num :: nums List(List(1,2),List(3,4)) flatMap { nums => List(10,20) withFilter { num => num > 15 } map { num => num :: nums } } To aid our understanding, here is a similar for comprehension that is simpler to understand, and that can be run at the REPL, together with its desugared version. List(List(20, 1, 2), List(20, 3, 4)) And here is the value produced by both the for comprehension and its desugared equivalent.
  • 21. Let’s see what the withFilter function does: • it is applied to a monad, in this case a List • it takes a predicate, i.e. a function that takes a parameter and returns either true or false. • it applies the predicate to each value yielded by the monad (in this case, each value contained in a List) • it filters out the values for which the predicate returns false • It lets through the values for which the predicate returns true On the next slide, we look at an example of how the behaviour of a for comprehension (or its desugared equivalent) is affected by the behaviour of the withFilter function. @philip_schwarz
  • 22. for nums <- List(List(1,2),List(3,4)) num <- List(10,20) if (num > 15) yield num :: nums List(List(1,2),List(3,4)) flatMap { nums => List(10,20) withFilter { num => num > 15 } map { num => num :: nums } } List(List(20, 1, 2), List(20, 3, 4)) for nums <- List(List(1,2),List(3,4)) num <- List(10,20) if (true) yield num :: nums List(List(1,2),List(3,4)) flatMap { nums => List(10,20) withFilter { num => true } map { num => num :: nums } } List(List(10, 1, 2), List(20, 1, 2), List(10, 3, 4), List(20, 3, 4)) for nums <- List(List(1,2),List(3,4)) num <- List(10,20) if (false) yield num :: nums List(List(1,2),List(3,4)) flatMap { nums => List(10,20) withFilter { num => false } map { num => num :: nums } } List() Notice that the predicate function passed to withFilter may or may not use its parameter. Like the map and flatMap functions, withFilter is used to bind the value(s) yielded by a monad (if any) to a variable name which is then available for use in subsequent computations. withFilter lets through some elements withFilter lets through all elements withFilter lets through no elements The difference is that while the functions passed to map and flatMap are almost certain to use the variable, in the case of withFilter, it may choose to influence the overall computation without actually using the variable.
  • 23. scala> List(1,2,3).withFilter(n => true).map(identity) val res0: List[Int] = List(1, 2, 3) scala> List(1,2,3).withFilter(n => n > 1).map(identity) val res1: List[Int] = List(2, 3) scala> List(1,2,3).withFilter(n => false).map(identity) val res2: List[Int] = List() Before we move on, here is a final, illustration of what the withFilter function does.
  • 24. Earlier we saw that the do expression that is the Haskell translation of the Scala for comprehension, involves the use of the guard function. Also, later we intend to look at how the Haskell do expression is implemented under the hood, which involves both the guard function and the (>>) function. So, in the next three slides, we are going to look at how Miran Lipovača explains guard and (>>).
  • 25. Here is the default implementation of the (>>) function: (>>) :: (Monad m) => ma -> mb -> mb m >> n = m >>= _ -> n Normally, passing some value to a function that ignores its parameter and always returns some predetermined value always results in that predetermined value. With monads however, their context and meaning must be considered as well. Here is how >> acts with Maybe: ghci> Nothing >> Just 3 Nothing ghci> Just 3 >> Just 4 Just 4 ghci> Just 3 >> Nothing Nothing Miran Lipovača ghci> [] >> [3] [] ghci> [3] >> [4] [4] ghci> [3] >> [] [] Here on the right is how >> acts with lists. >>= is Haskell’s equivalent of Scala’s flatMap and _ -> n is Haskell’s equivalent of Scala’s anonymous function _ => n. scala> List() >> List(3) val res0: List[Int] = List() scala> List(3) >> List(4) val res1: List[Int] = List(4) scala> List(3) >> List() val res2: List[Nothing] = List() scala> List() >> List() val res3: List[Nothing] = List() scala> None >> 3.some val res0: Option[Int] = None scala> 3.some >> 4.some val res1: Option[Int] = Some(4) scala> 3.some >> None val res2: Option[Nothing] = None scala> None >> None val res3: Option[Nothing] = None Same as above, but using the Scala Cats library: import cats._ import cats.implicits._
  • 26. MonadPlus and the guard Function List comprehensions allow us to filter our output. For instance, we can filter a list of numbers to search only for numbers whose digit contains a 7: ghci> [x | x <- [1..50], '7' `elem` show x] [7,17,27,37,47] We apply show to x to turn our number into a string, and then we check if the character ‘7’ is part of that string. To see how filtering in list comprehensions translates to the list monad, we need to check out the guard function and the MonadPlus type class. The MonadPlus type class is for monads that can also act as monoids. Here is its definition: class Monad m => MonadPlus m where mzero :: ma mplus :: ma -> ma -> ma mzero is synonymous with mempty from the Monoid type class, and mplus corresponds to mappend. Because lists are monoids as well as monads, they can be made an instance of this type class: instance MonadPlus [] where mzero = [] mplus = (++) For lists, mzero represents a nondeterministic computation that has no results at all – a failed computation. mplus joins two nondeterministic values into one. The guard function is defined like this: guard :: Bool -> m () guard True = return () guard False = mzero guard takes a Boolean value. If that value is True, guard takes a () and puts it in a minimal default context that succeeds. If the Boolean value is False, guard makes a failed monadic value. Miran Lipovača
  • 27. Here it is in action: ghci> guard (5 > 2) :: Maybe () Just () ghci> guard (1 > 2) :: Maybe () Nothing ghci> guard (5 > 2) :: [()] [()] ghci> guard (1 > 2) :: [()] [] This looks interesting, but how is it useful? In the list monad, we use it to filter out nondeterministic computations: ghci> [1..50] >>= (x -> guard ('7' `elem` show x) >> return x) [7,17,27,37,47] The result here is the same as the result of our previous list comprehension. How does guard achieve this? Let’s first see how guard functions in conjunction with >>: ghci> guard (5 > 2) >> return "cool" :: [String] ["cool"] ghci> guard (1 > 2) >> return "cool" :: [String] [] If guard succeeds, the result contained within it is the empty tuple. So then we use >> to ignore the empty tuple and present something else as the result. However, if guard fails, then so will the return later on, because feeding an empty list to a function with >>= always results in an empty list. guard basically says, “If this Boolean is False, then produce a failure right here. Otherwise, make a successful value that has a dummy result of () inside it.” All this does is to allow the computation to continue. Miran Lipovača ghci> [x | x <- [1..50], '7' `elem` show x] [7,17,27,37,47] same result
  • 28. for queens <- placeQueens(k - 1) queen <- 1 to n if safe(queen, queens) yield queen :: queens placeQueens(k - 1) flatMap { queens => (1 to n) withFilter { queen => safe(queen, queens) } map { queen => queen :: queens } } do queens <- placeQueens(k-1) queen <- [1..n] guard (safe queen queens) return (queen:queens) placeQueens(k-1) >>= queens -> [1..n] >>= queen -> guard (safe queen queens) >> return (queen:queens) Here we see the desugaring of the Scala for comprehension again, but we also desugar the equivalent Haskell do expression. Note how, while the desugared Scala code introduces the use of flatMap, withFilter and map, the desugared Haskell code introduces the use of >>= (Haskell’s equivalent of flatMap) and >>. Note also how, in Haskell, the guard function is used in both the do expression and in its desugared equivalent.
  • 29. for nums <- List(List(1,2),List(3,4)) num <- List(10,20) if (num > 15) yield num :: nums List(List(1,2),List(3,4)) flatMap { nums => List(10,20) withFilter { num => num > 15 } map { num => num :: nums } } do nums <- [[1,2],[3,4]] num <- [10,20] guard (num > 15) return (num:nums) [[1,2],[3,4]] >>= nums -> [10,20] >>= num -> guard (num > 15) >> return (num:nums) [[20,1,2],[20,3,4]] List(List(20, 1, 2), List(20, 3, 4)) Same as on the previous slide, except that here the logic is simpler to understand and can be run at the REPL. @philip_schwarz
  • 30. do nums <- [[1,2],[3,4]] num <- [10,20] guard (num > 15) return (num:nums) [[1,2],[3,4]] >>= nums -> [10,20] >>= num -> guard (num > 15) >> return (num:nums) List(List(20, 1, 2), List(20, 3, 4)) do nums <- [[1,2],[3,4]] num <- [10,20] guard (True) return (num:nums) [[1,2],[3,4]] >>= nums -> [10,20] >>= num -> guard (True) >> return (num:nums) List(List(10, 1, 2), List(20, 1, 2), List(10, 3, 4), List(20, 3, 4)) do nums <- [[1,2],[3,4]] num <- [10,20] guard (False) return (num:nums) [[1,2],[3,4]] >>= nums -> [10,20] >>= num -> guard (False) >> return (num:nums) List() guard lets through some elements guard lets through all elements guard lets through no elements
  • 31. ghci> [1,2] >>= n -> ['a',’b’] >>= ch -> return (n,ch) [(1,'a'),(1,'b'),(2,'a'),(2,'b’)] … … Here is the previous expression rewritten in do notation: listOfTuples :: [(Int,Char)] listOfTuples do n <- [1,2] ch <- [’a’,’b’] return (n,ch) Do Notation and List Comprehensions Using lists with do notation might remind you of something you’ve seen before. For instance, check out the following piece of code: ghci> [(n, ch) | n <- [1,2], ch <- ['a','b’]] [(1,'a'),(1,'b'),(2,'a'),(2,'b’)] Yes, list comprehensions! In our do notation example, n became every result from [1,2]. For every such result, ch was assigned a result from ['a','b’], and then the final line put (n,ch) into a default context (a singleton list) to present it as the result without introducing any additional nondeterminism. In this list comprehension, the same thing happened, but we didn’t need to write return at the end to present (n,ch) as the result, because the output part of a list comprehension did that for us. In fact, list comprehensions are just syntactic sugar for using lists as monads. In the end, list comprehensions and lists in do notation translate to using >>= to do computations that feature nondeterminism. Miran Lipovača
  • 32. do queens <- placeQueens(k-1) queen <- [1..n] guard (safe queen queens) return (queen:queens) [queen:queens | queens <- placeQueens(k-1), queen <- [1..n], safe queen queens] placeQueens(k-1) >>= queens -> [1..n] >>= queen -> guard (safe queen queens) >> return (queen:queens) We saw earlier what the Haskell do expression looks like when it is desugared. Desugaring the list comprehension produces the same result.
  • 33. queens n = placeQueens n where placeQueens 0 = [[]] placeQueens k = [queen:queens | queens <- placeQueens(k-1), queen <- [1..n], safe queen queens] safe queen queens = all safe (zipWithRows queens) where safe (r,c) = c /= col && not (onDiagonal col row c r) row = length queens col = queen onDiagonal row column otherRow otherColumn = abs (row - otherRow) == abs (column - otherColumn) zipWithRows queens = zip rowNumbers queens where rowCount = length queens rowNumbers = [rowCount-1,rowCount-2..0] def queens(n: Int): List[List[Int]] = def placeQueens(k: Int): List[List[Int]] = if k == 0 then List(List()) else for queens <- placeQueens(k - 1) queen <- 1 to n if safe(queen, queens) yield queen :: queens placeQueens(n) def onDiagonal(row: Int, column: Int, otherRow: Int, otherColumn: Int) = math.abs(row - otherRow) == math.abs(column - otherColumn) def safe(queen: Int, queens: List[Int]): Boolean = val (row, column) = (queens.length, queen) val safe: ((Int,Int)) => Boolean = (nextRow, nextColumn) => column != nextColumn && !onDiagonal(column, row, nextColumn, nextRow) zipWithRows(queens) forall safe def zipWithRows(queens: List[Int]): Iterable[(Int,Int)] = val rowCount = queens.length val rowNumbers = rowCount - 1 to 0 by -1 rowNumbers zip queens As a recap, let’s see again the translation of the whole program from Scala to Haskell.
  • 34. There is lots to do in part two. We’ll kick off by taking the Scala version of the program, and extending it so that it can display a single solution board as follows: To conclude the first part of this slide deck, see below for a simple way of displaying the solutions to the N-queens problem. We’ll then get the program to display, all together, the results of queens(N) for N = 4, 5, 6, 7, 8. Plus much more. See you then. @philip_schwarz