Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[error] Function Definition in GHCi on Linux 5.8 #59

Closed
nasirhm opened this issue Oct 2, 2020 · 4 comments
Closed

[error] Function Definition in GHCi on Linux 5.8 #59

nasirhm opened this issue Oct 2, 2020 · 4 comments
Labels
question Further information is requested

Comments

@nasirhm
Copy link

nasirhm commented Oct 2, 2020

Hey Awesome people, First of all thank you very much for the amazing idea of learning Haskell in 4 PRs with Hacktoberfest (I
absolutely love the Idea ❤️ )

I'm trying to define a function in Glassgow Haskell Compiler's REPL by executing the following line :

prelude> roundSubtract :: Double -> Int -> Int

and It results in the following error:

<interactive>:3:1: error:
    • No instance for (Show (Double -> Int -> Int))
        arising from a use of ‘print’
        (maybe you haven't applied a function to enough arguments?)
    • In a stmt of an interactive GHCi command: print it

On the tutorial, In Chapter1.hs, on line 358, It asks to do it.

CC: @chshersh @vrom911

@chshersh chshersh added the question Further information is requested label Oct 3, 2020
@chshersh
Copy link
Contributor

chshersh commented Oct 3, 2020

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 🙂

@vasylzhmurko
Copy link

Hello guys, maybe its possible to write function definition with ";" inline.
Example: myFunction :: String -> String; myFunction string = "Hello " ++ string

@chshersh
Copy link
Contributor

chshersh commented Oct 3, 2020

@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!

@chshersh chshersh closed this as completed Oct 3, 2020
@nasirhm
Copy link
Author

nasirhm commented Oct 3, 2020

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants