Forest
This is a forest with trees in all directions. To the east, there appears to be sunlight.
You hear in the distance the chirping of song bird.
Erase everything that you have written in the file QuestMain.hs. If you feel sorry for washing, then leave. Or use the version control system (git, svn): I assure you, the fear that you accidentally break the code will disappear forever! Any version of the code can be seen and restored whenever you want. Refactoring Haskell programs is a pleasure in and of itself, and with a version control system, it's not at all burdensome. Yes, refactoring can also be enjoyable! You rule, rule Haskell code so that it finally compiles, and when it compiles, it starts working! That part of the mistakes that you would make in an imperative language is simply impossible here. There are, of course, logic errors, but it is not hard to find and fix, but with the version control system it is even easier. In addition, logs with hundreds of other edits are a good example of how you did a good job, and the visible results from the path traveled motivate you to work further.
describeLocation locNumber = case locNumber of
1 -> "You are standing in the middle of the wooden table."
2 -> "You are standing behind a small wooden fence."
otherwise -> "Unknown location."
describeLocation :: Integer -> String
* Main > describeLocation 2.0
“You’re behind the small wooden fence.”
* Main > describeLocation 2.6
“Unknown location.”
* Main > : type describeLocation
describeLocation :: Num a => a -> [ Char ]
describeLocation :: Integer -> String
describeLocation locNumber = case locNumber of
1 -> "You are standing in the middle of the wooden table."
2 -> "You are standing behind a small wooden fence."
otherwise -> "Unknown location."
* Main > : t describeLocation
describeLocation :: Integer -> String
* Main > describeLocation 2
“You’re behind the small wooden fence.”
* Main > describeLocation 2.0
< interactive > : 1 : 18 :
No instance for ( Fractional Integer )
arising from the literal ' 2.0 '
Possible fix: add an instance declaration for ( Fractional Integer )
...
prod :: float -> float -> float
prod x y = x * y
The library documentation primarily provides a definition of the types of functions and a brief description of them. It may appear that types have no other tasks anymore; However, it is not. Types are the main data description objects; this is a higher abstraction over data. Using types, you can construct data structures of any complexity, create abstract types, set the behavior of code, check its correctness, plan future algorithms, influence their performance and semantics. If code is a program behavior, then data types are the content and structure of the program. And the data is the filling of the program. As we shall see, Haskell has a wonderful type system that is not only deep and expressive, but also supported by a powerful mathematical apparatus. It is convenient to work with types in Haskell, because they are based on several basic constructs that complement each other well.
describeLocation :: String -> String
describeLocation locName = case locName of
"Home" -> "You are standing in the middle of the wooden table."
"Friend's yard" -> "You’re behind the small wooden fence."
otherwise -> "Unknown location."
* Main > describeLocation "Home"
"You are standing in the middle of the room at the wooden table."
* Main > describeLocation "hOmE"
“Unknown location.”
upperCaseString :: String -> String
upperCaseString str = ............ - Somehow we make all the letters BIG.
describeLocation :: String -> String
describeLocation locName = case ( upperCaseString locName ) of
"HOME" -> "You are standing in the middle of the wooden table."
"FRIEND'S YARD" -> "You are standing behind a small wooden fence."
otherwise -> "Unknown location."
* Main > describeLocation "FRieNd'S yard"
“You’re behind the small wooden fence.”
* Main > describeLocation "hOMe"
"You are standing in the middle of the room at the wooden table."
Yeah, I see your curious eyes. Want function upperCaseString? Isn't it too early? Okay. I have nothing to hide. We need some function, "toUpper", in the standard Prelude module it is not. It is from the “Char” module, so it needs to be connected:import Char - At the beginning of QuestMain.hs we connect the Char module
upperCaseString :: String -> String
upperCaseString str = map toUpper str
I will not explain anything here! They themselves wanted to climb ahead - do it yourself and sort it out!
... Well, well, well, persuaded. In outline. The map function takes two arguments: the toUpper function and our str string. The map's task is simple: apply the toUpper function to each element of the str string. What elements does a string consist of? Correct, from characters. The toUpper function is applied to all these characters, which takes them to uppercase (well, if these are letters, of course).
home :: String
home = "HOME"
friend'sYard :: String - The apostrophe character (') can be used inside and at the end as a letter.
friend'sYard = "FRIEND'S YARD"
garden :: String
garden = "GARDEN"
* Main > describeLocation home
"You are standing in the middle of the room at the wooden table."
* Main > describeLocation friend'sYard
“You’re behind the small wooden fence.”
* Main > describeLocation garden
“Unknown location.”
home :: String
home = "You are standing in the middle of the middle of the wooden table."
friend'sYard :: String
friend'sYard = "This is a wooden wooden fence."
garden :: String
garden = "You are in the garden. Garden looks very well: clean, tonsured, cool and wet. "
describeLocation :: String -> String
describeLocation location = location
* Main > describeLocation garden
“You are in the garden. Garden looks very well: clean, tonsured, cool and wet. "
* Main > friend'sYard == "You’re behind the small wooden fence."
True
* Main > : t friend'sYard
friend'sYard :: [ Char ]
* Main > : t "You’re behind the small wooden fence."
“You’re behind the small wooden fence.” :: [ Char ]
Here, [Char] is the same as String. Literally, [Char] is a list of characters, a synonym for String. We could replace String with [Char] or even mix both types in one program, there would be no error. But it is more convenient to write a "string" than a "list of characters." We ourselves will set synonyms for many of our types. So, in my adv2game ObjectName is defined, also a string (also a list of characters). Looking at ObjectName, I understand that this is not just a string, but in it, in theory, should be the name of the object. Synonyms are defined by the type keyword, which works in the same way as typedef in C ++:type ObjectName = String
And this is how the String type is specified in the Prelude module:type String = [ Char ]
The square brackets say that this is a list from Char. Say “STRING” is the same as the list of characters: ['S', 'T', 'R', 'I', 'N', 'G']. It’s just that no one in their right mind would write a string like this, because in Haskell there is a simplification for the list of characters - “strings in quotes”.* Main > [ 'S' , 'T' , 'R' , 'I' , 'N' , 'G' ]
"STRING"
* Main > “I am a” ++ [ 'S' , 'T' , 'R' , 'I' , 'N' , 'G' ] ++ "."
"I am a STRING."
* Main > putStrLn [ 'S' , 'T' , 'R' , '\ n' , 'I' , 'N' , 'G' ]
STR
Ing
* Main > [ 'S' , 'T' , 'R' , 'I' , 'N' , 'G' ] == "STRING"
True
You can specify lists of anything: a list of integers [Integer], a list of strings [String] (which expands to a list of Char lists), and so on. Lists are the main structure in the FN, and we will learn a lot about them.
describeLocation :: ????? -> String
describeLocation loc = case loc of
Home -> "You are standing in the middle of the wooden table."
Friend'sYard -> “You ’ve been behind a small wooden fence.”
Garden -> “You are in the garden. Garden looks very well: clean, tonsured, cool and wet. "
otherwise -> "Unknown location."
data Location = Home | Friend'sYard | Garden
data Location =
Home
| Friend'sYard
| Garden
describeLocation :: Location -> String
describeLocation loc = case loc of
Home -> "You are standing in the middle of the wooden table."
Friend'sYard -> “You ’ve been behind a small wooden fence.”
Garden -> “You are in the garden. Garden looks very well: clean, tonsured, cool and wet. "
otherwise -> "Unknown location."
* Main > describeLocation Home
"You are standing in the middle of the room at the wooden table."
* Main > describeLocation Friend'sYard
“You’re behind the small wooden fence.”
describeHomeLocation = describeLocation Home
describeGardenLocation = describeLocation garDEN
describeGardenLocation ' = describeLocation GarDEN
* Main > : r
[ 1 of 1 ] Compiling Main ( H: \ Haskell \ QuestTutorial \ Quest \ QuestMain . Hs , interpreted )
H: \ Haskell \ QuestTutorial \ Quest \ QuestMain . hs: 15 : 43 :
Not in scope: 'garDEN'
H: \ Haskell \ QuestTutorial \ Quest \ QuestMain . hs: 16 : 44 :
Not in scope: data constructor 'GarDEN'
Failed , modules loaded: none .
data Location =
Location - This is correct, although it is not clear why.
| Home
| Friend'sYard
| Garden
- Where to go with the Walk or Go team.
data Direction = North | South | West | East
- Player's actions.
data Action = Look | Go | Inventory | Take | Drop | Investigate | Quit | Save | Load | New
* Main > North == North
< interactive > : 1 : 7 :
No instance for ( Eq Direction )
arising from a use of ' == '
Possible fix: add an instance declaration for ( Eq Direction )
In the expression: North == North
In this equation for 'it': it = North == North
More specifically, it asks you to add the Direction type to the Eq type class. Eq is a class of types for which the operations "==" and "/ =" are defined.
And now forget what is written in this box. It’s too early for us to talk about type classes .
data direction =
North
| South
| West
| East
deriving ( Eq ) - There should be indentation from the left edge.
* Main > North == North
True
* Main > North / = North
False
* Main > North == South
False
Task for fixing:
Think of the walk function - the function of travel between locations. It takes two parameters: the current location (Location) and direction (Direction), and returns a new location located in this direction. The route map for each location is as follows:
Home:
to the north - Garden
South - Friend'sYard
to the east - Home
to the west - Home
Garden:
to the north - Friend'sYard
to the south - Home
to the east - Garden
to the west - Garden
Friend'sYard:
to the north - Home
to the south - Garden
to the east - Friend'sYard
to the west - Friend'sYard
Source: https://habr.com/ru/post/121189/
All Articles