Zebra puzzle: Difference between revisions
Content deleted Content added
Added Wren |
|||
Line 2,587: | Line 2,587: | ||
=={{header|FormulaOne}}== |
=={{header|FormulaOne}}== |
||
<lang FormulaOne> |
<lang FormulaOne> |
||
// First, let's give some variables some values: |
// First, let's give some type-variables some values: |
||
Nationality = Englishman | Swede | Dane | Norwegian | German |
Nationality = Englishman | Swede | Dane | Norwegian | German |
||
Colour = Red | Green | Yellow | Blue | White |
Colour = Red | Green | Yellow | Blue | White |
||
Line 2,598: | Line 2,598: | ||
We use injections to make the array-elements unique. |
We use injections to make the array-elements unique. |
||
Example: 'Pet' is an array of unique elements of type 'Domestic', indexed by 'Nationality'. |
Example: 'Pet' is an array of unique elements of type 'Domestic', indexed by 'Nationality'. |
||
In the predicate 'Zebra', we use this injection 'Pet' to define the array-variable 'pet'. |
In the predicate 'Zebra', we use this injection 'Pet' to define the array-variable 'pet' as a parameter if the 'Zebra'-predicate. |
||
The symbol used is the '->>'. 'Nationality->>Domestic' can be read as 'Domestic(Nationality)' in "plain array-speak"; |
The symbol used is the '->>'. 'Nationality->>Domestic' can be read as 'Domestic(Nationality)' in "plain array-speak"; |
||
the difference being that the elements are by definition unique. |
the difference being that the elements are by definition unique (cf. 'injective function'). |
||
So, in FormulaOne we use a formula like: 'pet(Swede) = Dog', which simply means that the 'Swede' (type 'Nationality') |
So, in FormulaOne we use a formula like: 'pet(Swede) = Dog', which simply means that the 'Swede' (type 'Nationality') |
||
has a 'pet' (type 'Pet', |
has a 'pet' (type 'Pet', of type 'Domestic', indexed by 'Nationality'), which appears to be a 'Dog' (type 'Domestic'). |
||
Or, one could say that the 'Swede' has been mapped to the 'Dog' (Oh, well...). |
Or, one could say that the 'Swede' has been mapped to the 'Dog' (Oh, well...). |
||
} |
} |
||
Line 2,643: | Line 2,643: | ||
{ 5. The green house is immediately to the left of the white house: } |
{ 5. The green house is immediately to the left of the white house: } |
||
{ The local predicate 'LeftOf' determines the order: } |
{ The local predicate 'LeftOf' (see below) determines the order: } |
||
LeftOf(green_house, white_house, order) & |
LeftOf(green_house, white_house, order) & |
||
Line 2,655: | Line 2,655: | ||
smoke(yellow_house) = Dunhill & |
smoke(yellow_house) = Dunhill & |
||
{ 9. In the middle house they drink milk: } |
{ 9. In the middle house (third in the row) they drink milk: } |
||
drink(order(Third)) = Milk & |
drink(order(Third)) = Milk & |
||
Line 2,674: | Line 2,674: | ||
smoke(German) = Prince & |
smoke(German) = Prince & |
||
{15. The Norwegian lives next to the blue house |
{15. The Norwegian lives next to the blue house |
||
Cf. 10. "The Norwegian lives in the first house", so the blue house is the second house: } |
|||
⚫ | |||
so the blue house is the second house } |
|||
⚫ | |||
{16. They drink water in a house next to the house where they smoke Blend: } |
{16. They drink water in a house next to the house where they smoke Blend: } |
||
Line 2,735: | Line 2,734: | ||
pet(cat_keeper) = Cat & |
pet(cat_keeper) = Cat & |
||
pet(neighbour_dunhill_smoker) = Horse & |
pet(neighbour_dunhill_smoker) = Horse & |
||
houseColour(Englishman) = Red & |
houseColour(Englishman) = Red & |
||
pet(Swede) = Dog & |
pet(Swede) = Dog & |
||
Line 2,765: | Line 2,763: | ||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
houseColour |
houseColour=[ {Englishman} Red, {Swede} White, {Dane} Blue, {Norwegian} Yellow, {German} Green ] |
||
pet |
pet =[ {Englishman} Birds, {Swede} Dog, {Dane} Horse, {Norwegian} Cats, {German} Zebra ] |
||
smokes |
smokes =[ {Englishman} PallMall, {Swede} BlueMaster, {Dane} Blend, {Norwegian} Dunhill, {German} Prince ] |
||
drinks |
drinks =[ {Englishman} Milk, {Swede} Beer, {Dane} Tea, {Norwegian} Water, {German} Coffee ] |
||
order |
order =[ {First} Norwegian, {Second} Dane, {Third} Englishman, {Fourth} German, {Fifth}, Swede ] |
||
</pre> |
</pre> |
||