`

haskell - interactive evaluation at the prompt

阅读更多

Like most other language, haskell also has an interative prompt where you use repl way to quickly run and test your code. however, since haskell do no thave the statement ending sign such as the ; which is common in other languages, you may find yourselves lacking such a construct. 

However, there are some way that can help alleviate the problem, they are:

  • use of the let biding with the help of { and } and explicit  braces and semicolons
  • wrapping the statement in :{ and :}

The example that you can get from the haskell.org on topic : Interactive evaluation  at the prompt

2.4.2. Using do-notation at the prompt

GHCi actually accepts statements rather than just expressions at the prompt. This means you can bind values and functions to names, and use them in future expressions or statements.

The syntax of a statement accepted at the GHCi prompt is exactly the same as the syntax of a statement in a Haskell do expression. However, there's no monad overloading here: statements typed at the prompt must be in the IO monad.

Prelude> x <- return 42
Prelude> print x
42
Prelude>

The statement x <- return 42 means “execute return 42 in the IO monad, and bind the result to x”. We can then use x in future statements, for example to print it as we did above.

If -fprint-bind-result is set then GHCi will print the result of a statement if and only if:

  • The statement is not a binding, or it is a monadic binding (p <- e) that binds exactly one variable.

  • The variable's type is not polymorphic, is not (), and is an instance of Show

.

Of course, you can also bind normal non-IO expressions using the let-statement:

Prelude> let x = 42
Prelude> x
42
Prelude>

Another important difference between the two types of binding is that the monadic bind (p <- e) is strict (it evaluates e), whereas with the letform, the expression isn't evaluated immediately:

Prelude> let x = error "help!"
Prelude> print x
*** Exception: help!
Prelude>

Note that let bindings do not automatically print the value bound, unlike monadic bindings.

Hint: you can also use let-statements to define functions at the prompt:

Prelude> let add a b = a + b
Prelude> add 1 2
3
Prelude>

However, this quickly gets tedious when defining functions with multiple clauses, or groups of mutually recursive functions, because the complete definition has to be given on a single line, using explicit braces and semicolons instead of layout:

Prelude> let { f op n [] = n ; f op n (h:t) = h `op` f op n t }
Prelude> f (+) 0 [1..3]
6
Prelude>

To alleviate this issue, GHCi commands can be split over multiple lines, by wrapping them in :{ and :} (each on a single line of its own):

Prelude> :{
Prelude| let { g op n [] = n
Prelude|     ; g op n (h:t) = h `op` g op n t
Prelude|     }
Prelude| :}
Prelude> g (*) 1 [1..3]
6

Such multiline commands can be used with any GHCi command, and the lines between :{ and :} are simply merged into a single line for interpretation. That implies that each such group must form a single valid command when merged, and that no layout rule is used. The main purpose of multiline commands is not to replace module loading but to make definitions in .ghci-files (see Section 2.9, “The .ghci file”) more readable and maintainable.

Any exceptions raised during the evaluation or execution of the statement are caught and printed by the GHCi command line interface (for more information on exceptions, see the module Control.Exception in the libraries documentation).

Every new binding shadows any existing bindings of the same name, including entities that are in scope in the current module context.

WARNING: temporary bindings introduced at the prompt only last until the next :load or :reload command, at which time they will be simply lost. However, they do survive a change of context with :module: the temporary bindings just move to the new location.

HINT: To get a list of the bindings currently in scope, use the :show bindings command:

Prelude> :show bindings
x :: Int
Prelude>

HINT: if you turn on the +t option, GHCi will show the type of each variable bound by a statement. For example:

Prelude> :set +t
Prelude> let (x:xs) = [1..]
x :: Integer 

xs :: [Integer]

 

2.4.3. Multiline input

Apart from the :{ ... :} syntax for multi-line input mentioned above, GHCi also has a multiline mode, enabled by :set +min which GHCi detects automatically when the current statement is unfinished and allows further lines to be added. A multi-line input is terminated with an empty line. For example:

Prelude> :set +m
Prelude> let x = 42
Prelude|

Further bindings can be added to this let statement, so GHCi indicates that the next line continues the previous one by changing the prompt. Note that layout is in effect, so to add more bindings to this let we have to line them up:

Prelude> :set +m
Prelude> let x = 42
Prelude|     y = 3
Prelude| 
Prelude>

Explicit braces and semicolons can be used instead of layout, as usual:

Prelude> do {
Prelude| putStrLn "hello"
Prelude| ;putStrLn "world"
Prelude| }
hello
world
Prelude>

Note that after the closing brace, GHCi knows that the current statement is finished, so no empty line is required.

Multiline mode is useful when entering monadic do statements:

Control.Monad.State> flip evalStateT 0 $ do
Control.Monad.State| i <- get
Control.Monad.State| lift $ do
Control.Monad.State|   putStrLn "Hello World!"
Control.Monad.State|   print i
Control.Monad.State|
"Hello World!"
0
Control.Monad.State>

During a multiline interaction, the user can interrupt and return to the top-level prompt.

Prelude> do
Prelude| putStrLn "Hello, World!"
Prelude| ^C
Prelude>

 

A running example 

NOTE: the following is only workable on a ghci/7.6.1

Prelude>:set +m

 

Below I have an running example

 

Prelude>import Data.Char (chr, ord)
Prelude Data.Char> :{
Prelude Data.Char| let {
Prelude Data.Char|   encode :: Int -> String -> String
Prelude Data.Char|  ;encode shift msg =
Prelude Data.Char|      let ords = map ord msg
Prelude Data.Char|          shifted = map (+shift) ords
Prelude Data.Char|      in map chr shifted;
Prelude Data.Char| }
Prelude Data.Char| :}
Prelude Data.Char> encode 3 "Hello world"
"Khoor#zruog"

 

you can also do this (with the do monad)

Prelude Data.Char> do {
Prelude Data.Char|    let { encode :: Int -> String -> String;
Prelude Data.Char|    encode shift msg =
Prelude Data.Char|     let ords = map ord msg
Prelude Data.Char|         shifted = map (+shift) ords
Prelude Data.Char|     in map chr shifted;
Prelude Data.Char|    };
Prelude Data.Char|    encode 3 "hello world";
Prelude Data.Char| }
"khoor#zruog"

 A note, " The last statement in a 'do' block must be an expression", you cannot do this..

Prelude Data.Char> do {
Prelude Data.Char|   let { encode :: Int -> String -> String;
Prelude Data.Char|   encode shift msg =
Prelude Data.Char|    let ords = map ord msg
Prelude Data.Char|        shifted = map (+shift) ords
Prelude Data.Char|    in map chr shifted;
Prelude Data.Char|    };
Prelude Data.Char| }

<interactive>:152:3:
    The last statement in a 'do' block must be an expression
      let encode :: Int -> String -> String
          encode shift msg = let ... in map chr shifted

 

if you have multiple line enabled, you can just write with one let statement. as follow. 

 

Prelude Data.Char> let {
Prelude Data.Char| encode :: Int -> String -> String
Prelude Data.Char| ;encode shift msg =
Prelude Data.Char| let ords = map ord msg
Prelude Data.Char|     shifted = map (+shift) ords
Prelude Data.Char| in map chr shifted;
Prelude Data.Char| }

 

 Cross references:

分享到:
评论

相关推荐

    Atom-ide-haskell-hoogle,在光标下显示符号的滚动信息。对原子的贡献.zip

    Atom-ide-haskell-hoogle 是一个专门为 Atom 编辑器设计的插件,它整合了 Haskell 的 Hoogle 工具,以提供强大的代码提示和搜索功能。Atom 是一款由 GitHub 开发的开源文本编辑器,它利用现代 web 技术如 HTML、CSS ...

    haskell-mode emacs

    在 Emacs 中,`haskell-mode` 是一个专门为了提升 Haskell 开发体验而设计的模式。 `haskell-mode` 提供了多种增强功能,旨在帮助 Haskell 开发者更高效地编写、调试和理解代码。这个模式包括以下关键特性: 1. **...

    haskell-chart, haskell的2D 图表库.zip

    在数据可视化领域,`haskell-chart`库提供了一种高效且灵活的方式来创建2D图表,这对于数据分析、科学计算以及教学等场景非常有用。这个库是开源的,意味着任何人都可以查看其源代码,学习并贡献改进。 `haskell-...

    Atom-haskell-ghc-mod,哈斯克尔.zip

    **哈斯克尔编程语言与Atom-Haskell-GHC-Mod** 哈斯克尔(Haskell)是一种纯函数式编程语言,以其优雅的语法、强静态类型系统和编译时优化而受到程序员的喜爱。它鼓励使用不可变数据和惰性求值,这使得哈斯克尔在...

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码.zip

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...

    Atom-haskell-debug,使用ghci在atom中实现图形haskell调试器.zip

    Atom-Haskell-Debug是针对Haskell开发者的一个强大工具,它允许你在流行的Atom文本编辑器中集成一个图形化的Haskell调试器。这个工具基于GHCi(Glasgow Haskell Compiler Interface),GHCi是Haskell的交互式环境,...

    haskell-ghc-mod:haskell-ghc-mod原子包

    从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...

    haskell-brainfuck:Haskel 脑残翻译

    你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...

    haskell-ghc-mod:haskell-ghc-mod原子包

    haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...

    Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid

    ### Haskell - The Craft of Functional Programming #### 一、概述 《Haskell - The Craft of Functional Programming》是一本深入探讨Haskell编程语言的经典著作。本书由Tantanoid编写,并于1999年由Addison-...

    Get Programming with HASKELL-2018-英文版.pdf

    Get Programming with HASKELL-2018-英文版

    haskell-relational-record-driver-mysql:用于 haskell-relational-record 的 MySQL 驱动程序

    用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...

    Atom-atom-haskell-scry,扩散系数.zip

    【标题】:“Atom-atom-haskell-scry,扩散系数.zip” 涉及的主要知识点是 Atom 编辑器与 Haskell 语言的集成以及 SCRY 工具的使用。 【描述】:“Atom-atom-haskell-scry.zip”的描述指出,这个压缩包包含了一个名...

    Atom-ide-haskell-repl,原子中的ghci repl。对原子的贡献.zip

    Atom-ide-haskell-repl是针对Atom文本编辑器的一个扩展插件,专为Haskell编程语言提供集成的GHCi(Glasgow Haskell Compiler Interface)交互式环境,即REPL(Read-Eval-Print Loop)。这个插件允许开发者在Atom编辑...

    haskell-dev-tools:我用来安装升级与Haskell相关的开发工具的元项目

    在Haskell的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...

    haskell-dap:Haskell DAP接口数据的实现

    Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...

    haskell-tools:Haskell的开发人员工具

    "haskell-tools"就是这样一个项目,它专注于为Haskell开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...

    函数式编程-haskell-to-java

    ### 函数式编程:Haskell到Java的转换 #### 概述 本文旨在探讨函数式编程语言Haskell如何被编译或转换为Java语言。Haskell作为一种纯函数式编程语言,以其强大的类型系统、惰性求值机制以及高度抽象的能力在学术界...

    Atom-ide-haskell-cabal,IDE的Cabal后端提供商.zip

    Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。

    haskell-lsp-client:haskell-lsp的客户端库

    haskell-lsp-client 该软件包适用于希望使其文本编辑器与兼容的文本编辑器的开发人员。 我已经开发了此软件包,并计划将其集成到。 示例客户端 该存储库中包含一个示例客户端。 此示例客户端仅运行并打开在命令行...

Global site tag (gtag.js) - Google Analytics