Code Monkey home page Code Monkey logo

Comments (4)

chshersh avatar chshersh commented on May 22, 2024 1

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.

nasirhm avatar nasirhm commented on May 22, 2024 1

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.

vasylzhmurko avatar vasylzhmurko commented on May 22, 2024

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

from learn4haskell.

chshersh avatar chshersh commented on May 22, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.