Dinesman's multiple-dwelling problem: Difference between revisions

Content added Content deleted
(Added 11l)
Line 1,856: Line 1,856:
print dinesman -- print all solutions (only one): [(3,2,4,5,1)]</lang>
print dinesman -- print all solutions (only one): [(3,2,4,5,1)]</lang>


Or as a list comprehension:
Or as a list comprehension (syntactic sugar for a list monad):
<lang haskell>import Data.List (permutations)
<lang haskell>import Data.List (permutations)


Line 1,862: Line 1,862:
main =
main =
print
print
[ ( "Baker lives on " ++ show b
[ ( "Baker lives on " <> show b,
, "Cooper lives on " ++ show c
"Cooper lives on " <> show c,
, "Fletcher lives on " ++ show f
"Fletcher lives on " <> show f,
, "Miller lives on " ++ show m
"Miller lives on " <> show m,
, "Smith lives on " ++ show s)
"Smith lives on " <> show s
)
| [b, c, f, m, s] <- permutations [1 .. 5]
| [b, c, f, m, s] <- permutations [1 .. 5],
, b /= 5
, c /= 1
b /= 5,
, f /= 1
c /= 1,
, f /= 5
f /= 1,
, m > c
f /= 5,
, abs (s - f) > 1
m > c,
, abs (c - f) > 1 ]</lang>
abs (s - f) > 1,
abs (c - f) > 1
]</lang>
{{out}}
{{out}}
<pre>[("Baker lives on 3","Cooper lives on 2","Fletcher lives on 4","Miller lives on 5","Smith lives on 1")]</pre>
<pre>[("Baker lives on 3","Cooper lives on 2","Fletcher lives on 4","Miller lives on 5","Smith lives on 1")]</pre>