小编Eya*_*yal的帖子

如何在Haskell中正确使用Control.Exception.catch?

有人可以解释以下ghci中行为与行之间的区别:

catch (return $ head []) $ \(e :: SomeException) -> return "good message"
Run Code Online (Sandbox Code Playgroud)

回报

"*** Exception: Prelude.head: empty list
Run Code Online (Sandbox Code Playgroud)

catch (print $ head []) $ \(e :: SomeException) -> print "good message"
Run Code Online (Sandbox Code Playgroud)

回报

"good message"
Run Code Online (Sandbox Code Playgroud)

为什么不是第一个抓住异常的案例?他们为什么不同?为什么第一个案例在异常消息之前加上双引号?

谢谢.

haskell exception

21
推荐指数
3
解决办法
2057
查看次数

如何生成随机的类型函数

我想以编程方式生成随机Haskell函数并对其进行评估.在我看来,唯一的方法是基本上以编程方式生成Haskell代码并使用GHC API或外部进程运行它,返回一个字符串,并将其解析回Haskell数据类型.这是真的?

我的理由如下.函数是多态的,所以我不能使用Typeable.更重要的是,即使我编写自己的类型检查器并使用其类型注释每个函数,我也无法向Haskell编译器证明我的类型检查器是正确的.例如,当我从一个异构函数集中提取两个函数并将一个函数应用于另一个函数时,我需要为编译器提供一个保证,即我用来选择这些函数的函数只选择具有相应类型的函数.但是没有办法做到这一点,对吧?

haskell typechecking

18
推荐指数
1
解决办法
1822
查看次数

GHCi中fromIntegral的行为不一致

当我使用fromIntegral函数时,我希望有人可以在GHCi中解释以下行为:

Prelude> let x = 1 :: Integer                                                                                                                                                    
Prelude> :t x                                                                                                                                                                    
x :: Integer                                                                                                                                                                     
Prelude> sqrt $ fromIntegral x                                                                                                                                                   
1.0                                                                                                                                                                              
Prelude> let y = fromIntegral x                                                                                                                                                  
Prelude> sqrt y                                                                                                                                                                  

<interactive>:181:1:                                                                                                                                                             
No instance for (Floating Integer)                                                                                                                                           
  arising from a use of `sqrt'                                                                                                                                               
Possible fix: add an instance declaration for (Floating Integer)                                                                                                             
In the expression: sqrt y                                                                                                                                                    
In an equation for `it': it = sqrt y                                                                                                                                         
Run Code Online (Sandbox Code Playgroud)

为什么我设置y然后采取它sqrt或直接采取sqrt

haskell types ghci

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

如何在Haskell中构建一个非确定性状态monad?

我想在Haskell中构建一个不确定的状态monad.这将允许我使用构建状态生成搜索空间中的所有元素以修剪不良位置.假设我有以下(伪)代码:

primitives :: [State Int Element] 
primitives = [... list of primitive stateful elements ...]                                                                                                                      
combine :: Element -> Element -> State Int Element                                                                                                            
expand :: Depth -> [State Int Element]                                                                                                                        
expand 0 = primitives                                                                                                                                         
expand d = do                                                                                                                                                 
  ... do something to the state ...                                                                                                                           
  left <- expand (d-1)                                                                                                                                        
  right <- expand (d-1)                                                                                                                                       
  let out = combine left right                                                                                                                                
  guard ( ... some check on out ... )                                                                                                                         
  return out        
Run Code Online (Sandbox Code Playgroud)

这里有几件事情是行不通的:我需要了解的最基本的事情是如何做一些陈述,然后将其传递给每个expand分支.我已经尝试了一些类型函数的方法,State Int [ State Int Element] …

haskell non-deterministic state-monad

3
推荐指数
1
解决办法
422
查看次数

如何在Haskell中为b-> a创建一个Show实例?

我正在尝试构造一个本质上是二叉树的数据类型,其中:每个节点的左分支是一个函数,可以作用于每个节点的右分支中的变量.我是Haskell的新手,我不确定我是否会以正确的方式解决这个问题,但我目前的问题是我无法弄清楚如何将我的类型添加到Show类型类中.这是我的尝试:

{-# LANGUAGE ExistentialQuantification #-}
-- file: TS.hs                                                                                                                                                       

data TypeSentence a = forall b. Apply (TypeSentence (b->a)) (TypeSentence b)
                    | Expr a

instance (Show a) => (Show (TypeSentence a)) where
        show (Expr x) = show x
        show (Apply x y) = (show x) ++ " " ++ (show y)

instance (Show (TypeSentence b->a)) where
    show (Expr x) = show "hello"

x = Expr 1
f = Expr (+1)
s = Apply f x
Run Code Online (Sandbox Code Playgroud)

但是,当我将其加载到ghci时,我收到以下错误:

TS.hs:9:24:                                                                                                                                                          
     Could not deduce (Show (b …
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
1
解决办法
1814
查看次数

如何从超类方法调用python子类方法?

我有以下类型的超类/子类设置:

class SuperClass(object):
    def __init__(self):
        self.do_something() # requires the do_something method always be called

    def do_something(self):
        raise NotImplementedError

class SubClass(SuperClass):
    def __init__(self):
        super(SuperClass, self).__init__() # this should do_something 

    def do_something(self):
        print "hello"
Run Code Online (Sandbox Code Playgroud)

我希望SuperClass init始终调用一个尚未实现的do_something方法.我正在使用python 2.7.也许ABC可以做到这一点,但它有另一种方式吗?

谢谢.

python subclass init abc super

0
推荐指数
1
解决办法
2544
查看次数