Dinesman's multiple-dwelling problem: Difference between revisions
Content deleted Content added
m →{{header|Icon}} and {{header|Unicon}}: remove [] |
added haskell |
||
Line 144: | Line 144: | ||
}</lang> |
}</lang> |
||
<pre>[2, 1, 3, 4, 0]</pre> |
<pre>[2, 1, 3, 4, 0]</pre> |
||
=={{header|Haskell}}== |
|||
The List monad is perfect for this kind of problem. One can express the problem statements in a very natural and concise way: |
|||
{{works with|GHC|6.10+}} |
|||
<lang haskell>import Data.List (permutations) |
|||
import Control.Monad (guard) |
|||
dinesman :: [(Int,Int,Int,Int,Int)] |
|||
dinesman = do |
|||
-- baker, cooper, fletcher, miller, smith are integers representing |
|||
-- the floor that each person lives on, from 1 to 5 |
|||
-- Baker, Cooper, Fletcher, Miller, and Smith live on different floors |
|||
-- of an apartment house that contains only five floors. |
|||
[baker, cooper, fletcher, miller, smith] <- permutations [1..5] |
|||
-- Baker does not live on the top floor. |
|||
guard $ baker /= 5 |
|||
-- Cooper does not live on the bottom floor. |
|||
guard $ cooper /= 1 |
|||
-- Fletcher does not live on either the top or the bottom floor. |
|||
guard $ fletcher /= 5 && fletcher /= 1 |
|||
-- Miller lives on a higher floor than does Cooper. |
|||
guard $ miller > cooper |
|||
-- Smith does not live on a floor adjacent to Fletcher's. |
|||
guard $ abs (smith - fletcher) /= 1 |
|||
-- Fletcher does not live on a floor adjacent to Cooper's. |
|||
guard $ abs (fletcher - cooper) /= 1 |
|||
-- Where does everyone live? |
|||
return (baker, cooper, fletcher, miller, smith) |
|||
main :: IO () |
|||
main = do |
|||
print $ head dinesman -- print first solution: (3,2,4,5,1) |
|||
print dinesman -- print all solutions (only one): [(3,2,4,5,1)]</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |