Comments (4)
Hi @nasirhm! When the section describes top-level function definitions and type-signatures, it talks about writing them inside a file, not inside REPL. GHCi can parse only one line at a time by default. But a function definition requires two lines: a type signature and a function body. That's why creating new functions in GHCi is awkward, and the course assumes that you will implement new functions inside a module, and then load the module in GHCi.
If you start fresh GHCi, and try to insert this string, you will get the following error:
Prelude> roundSubtract :: Double -> Int -> Int
<interactive>:8:1: error:
Variable not in scope: roundSubtract :: Double -> Int -> Int
What you probably did, is that you've copied the implementation of roundSubtract
without the type, GHCi inferred its type and recognized as valid, and now you're trying to show a value roundSubtract
which is a function, but Haskell can't display functions in runtime.
Let me know if clarifies some things, or whether you need more help 🙂
from learn4haskell.
GHCi can parse only one line at a time by default.
Nice catch, Will keep it in mind for the next examples 👍
Thank You for answering and information @vasylzhmurko and @chshersh :)
from learn4haskell.
Hello guys, maybe its possible to write function definition with ";" inline.
Example: myFunction :: String -> String; myFunction string = "Hello " ++ string
from learn4haskell.
@vasylzhmurko Yes, you can write a function type signature and its implementation in a single line by separating type signature and body with a semicolon ;
. Exactly as @vasylzhmurko wrote, this is valid Haskell syntax inside GHCi.
You can also use special GHCi commands :{
and :}
to start and end multiline blocks, we mention them in Chapter Three.
Defining functions in one-line in GHCi sometimes useful, but it's not how you usually write them in the module. Also, once you start having more complex bodies (guards, nested if-then-else expressions, etc.), writing everything in a single line becomes very awkward.
I'm closing this issue, as the question seems to be answered 🙂 Let us know if you have other questions!
from learn4haskell.
Related Issues (20)
- Bug in est/Test/Chapter1.hs ? HOT 3
- Use default main branch
- Unable to open pull request for Chapter 4 HOT 2
- Build dependency happy fails on OSX 10.15.7 HOT 2
- Update GHC to 8.10.4
- [RFC] Create certificate when all Chapters are passed HOT 1
- Chapter 3 knight Monster clarification HOT 2
- Create 2021 Hacktoberfest logo
- Chapter2: takeEven or dropEven? HOT 1
- Odd test output results HOT 4
- Question about a test HOT 2
- Useful to have docker instructions in README.md? HOT 3
- Is this wording about typeclasses correct in Chapter3.hs? HOT 1
- Save @vrom911's and @chshersh's time by including solutions HOT 1
- are my solutions be mentored ? HOT 1
- Error resolving dependencies while make test-chapter1-basic. HOT 10
- Typo in chapter 1: `Add` instead of `add`
- Upgrade to GHC 9.2
- Upgrade to GHC-9.4
- [Chapter 4] Is it possible to have an instance of Functor for "Trap e" of "Secret e a"?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from learn4haskell.