小编Z-Y*_*Y.L的帖子

Why the difference in type-inference over the as-pattern in two similar function definitions?

I have the following two similar function definitions:

left f (Left x) = Left (f x)
left _ (Right x) = Right x

left' f (Left x) = Left (f x)
left' _ r@(Right _) = r
Run Code Online (Sandbox Code Playgroud)

When I check the type signatures of the two functions, I am confused by the following types:

ghci> :t left
left :: (t -> a) -> Either t b -> Either a b
ghci> :t left'
left' :: (a -> a) -> Either …
Run Code Online (Sandbox Code Playgroud)

haskell type-signature

26
推荐指数
2
解决办法
619
查看次数

为什么`data`导致无限循环而`newtype`不是

我正在学习带有箭头Arrow的教程编程。我已经根据论文输入了以下代码,除了的SF定义是由data,而不是论文中所定义的newtype实际上,由于我是从内存中键入代码的,所以我偶然地做了此更改):

import Control.Category
import Control.Arrow
import Prelude hiding (id, (.))

data SF a b = SF { runSF :: [a] -> [b] }  -- this is the change, using data instead of newtype as in the paper 

-- The folowing code is the same as in the paper
instance Category SF where
  id = SF $ \x -> x
  (SF f) . (SF g) = SF $ …
Run Code Online (Sandbox Code Playgroud)

haskell arrows newtype

9
推荐指数
1
解决办法
345
查看次数

如何摆脱已编译的Haskell GUI应用程序的黑色控制台窗口

我编了Haskell的GUI应用程序stack-1.7.1,ghc-8.2.2,gtk+-2.24.28glade-3.8.当我运行可执行文件时,黑色控制台窗口始终跟随GUI窗口.我想摆脱它,但我没有找到任何关于Haskell GUI应用程序的提示.我还是Haskell的新手,尤其是Haskell编译.所以我很抱歉,如果这是一项简单的任务,但如果有人能给我一些提示我很感激.

gtk user-interface haskell glade

8
推荐指数
1
解决办法
87
查看次数

如何理解类型 a 和 forall r。(a -> r) -> r 是同构的

Thinking with Types一书中,6.4 Continuation Monad讲到了类型aforall r. (a -> r) -> r是同构的,可以通过以下函数来见证:

cont :: a -> (forall r. (a -> r) -> r)
cont x = \f -> f x

unCont :: (forall r. (a -> r) -> r) -> a
unCont f = f id
Run Code Online (Sandbox Code Playgroud)

在这本书中,它告诉我们任何具有相同基数的两种类型总是彼此同构的。所以我试图找出类型a和的基数forall r. (a -> r) -> r

假设类型的基数a|a|。那么对于类型forall r. (a -> r) -> r,如何计算其基数等于|a| …

haskell types isomorphism

8
推荐指数
2
解决办法
141
查看次数

如何在 Powershell 中设置彩色 GHCi 提示符

我在网上搜索了解决方案,找到了一些有用的信息,如下:

  1. 根据Glenn Slayden的回答,在 Win10 中设置默认启用 ANSI 终端控制。

  2. ghci.conf根据这里这里在配置文件中设置提示

以下是我的配置:

:set prompt "\ESC[33mImported Modules: %s\n\ESC[34m\x03BB> \ESC[0m"
:set prompt-cont "\ESC[31m > \ESC[0m"
Run Code Online (Sandbox Code Playgroud)

加载时ghci,不起作用(见下面的截图)

在此输入图像描述

看来只有第一个 ASCII 转义序列\ESC[33m生效,其余序列都没有渲染。

接下来我根据wiki进行如下修改:

:set prompt "\ESC[33m\STXImported Modules: %s\n\ESC[34m\STX\x03BB> \ESC[0m\STX"
:set prompt-cont "\ESC[31m\STX > \ESC[0m\STX"
Run Code Online (Sandbox Code Playgroud)

更好了,但似乎仍然只有第一个转义序列有效,而其他序列被忽略。

在此输入图像描述

这次我猜测转义序列的渲染可能与转义字符有关\n,所以我\n在每个序列的末尾添加并删除,\STX如下所示:

:set prompt "\ESC[33mImported Modules: %s\n\ESC[34m\x03BB> \ESC[0m\n"
:set prompt-cont "\ESC[31m > \ESC[0m\n"
Run Code Online (Sandbox Code Playgroud)

现在它可以按预期工作,但必须引入一些额外的\n,这会导致布局丑陋。

在此输入图像描述

现在我不知道为什么需要一些额外的东西\n才能在 GHCi 提示符中正确渲染转义序列以及如何解决这个问题。

感谢您提供任何信息!

haskell prompt colors ghci

5
推荐指数
0
解决办法
406
查看次数

`onException`的行为如何

finally并且onException是模块中的两个函数Control.Exception,它们具有相同的签名但行为不同. 是文件.因为finally,它说:

finally 
:: IO a  -- computation to run first
-> IO b  -- computation to run afterward (even if an exception was raised)
-> IO a
Run Code Online (Sandbox Code Playgroud)

虽然因为onException,它说:

喜欢finally,但只有在计算引发异常时才执行最终操作.

所以我做了以下测试:

ghci> finally (return $ div 4 2) (putStrLn "Oops!")
Oops!
2
ghci> finally (return $ div 4 0) (putStrLn "Oops!")
Oops!
*** Exception: divide by zero
Run Code Online (Sandbox Code Playgroud)

这是按预期行事的.

但是,onException不是:

ghci> onException (return $ div 4 2) …
Run Code Online (Sandbox Code Playgroud)

haskell exception

4
推荐指数
1
解决办法
72
查看次数

找不到与参数名称“ TokenKind”匹配的参数

根据此博客,我已经美化了Powershell ,但是OperatorParameter如下所示为灰色:

在此处输入图片说明 在此处输入图片说明

所以我通过Set-PSReadlineOption以下方式更改它们的颜色:

Set-PSReadlineOption -TokenKind Operator -ForegroundColor Yellow
Run Code Online (Sandbox Code Playgroud)

但出现以下错误:

Set-PSReadLineOption:找不到与参数名称“ TokenKind”匹配的参数

???? ?:1 ??:22

  • Set-PSReadlineOption -TokenKind运算符-ForegroundColor黄色
    • CategoryInfo:InvalidArgument:(:) [Set-PSReadLineOption]?ParameterBindingException
    • FullyQualifiedErrorId:NamedParameterNotFound,Microsoft.PowerShell.SetPSReadLineOption

但是显示的帮助文件Set-PSReadlineOption显示它具有一个TokenKind参数,Operator而该参数又可以作为其参数。

我很困惑为什么会发生此错误。

我的powershell版本是

在此处输入图片说明

感谢您的任何建议!

powershell psreadline

4
推荐指数
1
解决办法
2692
查看次数