小编Ben*_*son的帖子

Haskell美元运算符应用程序

我无法理解函数应用程序如何与haskell中的currying一起工作.如果我有以下功能:

($) :: (a -> b) -> a -> b
Run Code Online (Sandbox Code Playgroud)

我明白要部分应用这个功能,我需要提供(a -> b)函数($第一个参数).

为什么可以先应用一个值(即反向参数)?

($ 0) :: Num a => (a -> b) -> b
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

haskell operators partial-application dollar-sign operator-sections

11
推荐指数
1
解决办法
1903
查看次数

Haskell运算符的交换属性?

有没有办法说明运算符是可交换的,所以我不必为两个方向给出相同的定义?例如:

data Nat = Zero | Succ Nat

(+) :: Nat -> Nat -> Nat
Zero + x = x
x + Zero = x
...
Run Code Online (Sandbox Code Playgroud)

在这里,有没有一种方法可以让我不必同时给出这两种定义,其中一种定义会从另一种定义中暗示出来?有没有办法说明这一点fn = flip fn

haskell operators pattern-matching commutativity

11
推荐指数
1
解决办法
362
查看次数

Python - 直到按键或超时才做某事

我几乎已经解决了这个问题,但我认为我需要朝着正确的方向努力.

我想要做的事每五秒钟,直到或者一定的时间量已过去用户中断它(在这种情况下,完成循环的该迭代完成之前).

import time
import threading

def do_something():
    T0 = time.clock()
    while (time.clock() - T0) < 60 and not e.isSet(): #as long as 60s haven't elapsed
                                                      #and the flag is not set
        #here do a bunch of stuff
        time.sleep(5)

thread = threading.Thread(target=do_something, args=())
thread.start()
e = threading.Event()

while thread.isAlive():
    #here I want the main thread to wait for a keypress and, if it receives it,
    #set the event e, which will cause the thread to finish …
Run Code Online (Sandbox Code Playgroud)

python multithreading timeout keypress wait

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

Python中的"Eratosthenes的真正筛选" - 为什么heapq比dict慢?

M. O'Neill的伟大论文之后,我尝试在Python中实现一些懒惰的,无限版的Eratosthenes Sieve.我惊讶地发现,基于堆的版本,该论文声称应该运行得更快,实际上对我来说实际上慢了两倍.

本文包含两个例子,一个基于dict,我已翻译(来自Haskell),因此:

from itertools import count
def dict_sieve():
    yield 2
    yield 3
    candidates = count(5, 2)
    composites = {9:{3}}  # map composites to their prime factors

    for candidate in candidates:
        try:
            factors = composites.pop(candidate)
        except KeyError:  # if it's not in the dict, it's prime
            yield candidate
            composites[candidate**2] = {candidate}  # Euler's optimization: start from prime**2
        else:
            for prime in factors:  # go through the prime factors and increment their keys
                try:
                    composites[candidate+prime*2].add(prime)  # use prime*2 …
Run Code Online (Sandbox Code Playgroud)

python heap performance sieve-of-eratosthenes

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

Agda:为Conor的堆栈示例运行函数

在ICFP 2012上,Conor McBride发表了主题演讲,主题为"Agda-curious?".

它具有小型堆栈机器实现.

视频在youtube上:http://www.youtube.com/watch?v = XGyJ519RY6Y

代码也在线:http: //personal.cis.strath.ac.uk/conor.mcbride/ICFP12Code.tgz

我想知道run第5部分的功能(如果您下载了代码,则为"Part5Done.agda").在run解释函数之前,谈话停止.

data Inst : Rel Sg SC Stk where
  PUSH  : {t : Ty} (v : El t) -> forall {ts vs} ->
            Inst (ts & vs) ((t , ts) & (E v , vs))
  ADD   : {x y : Nat}{ts : SC}{vs : Stk ts} ->
            Inst ((nat , nat , ts) & (E y , E x , vs))
              ((nat …
Run Code Online (Sandbox Code Playgroud)

agda

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

如何在IPython笔记本中编写内联乳胶代码

这可能是非常明显的但是如何在IPython笔记本文件中内联编写Latex脚本,所以在解析时它不会启动新行?

python latex ipython

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

在monad变换器中,为什么已知的monad是内部的?

例如,MaybeT定义为:

newtype MaybeT m a =
  MaybeT { runMaybeT :: m (Maybe a)}
Run Code Online (Sandbox Code Playgroud)

但不是:

newtype MaybeT m a =
  MaybeT { runMaybeT :: Maybe (m a) }
Run Code Online (Sandbox Code Playgroud)

为什么是这样?

monads haskell monad-transformers

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

为什么编译器无法将类型'a == a'与'True'匹配为类型系列?

是否有一些原因导致此代码未编译:

type family Foo a b :: Bool where
    Foo a b = a == b

foo :: Foo a b ~ True => Proxy a -> Proxy b
foo _ = Proxy

bar :: Proxy a -> Proxy a
bar = foo
Run Code Online (Sandbox Code Playgroud)

有错误:

Couldn't match type ‘a == a’ with ‘'True’
Expected type: 'True
  Actual type: Foo a a
Run Code Online (Sandbox Code Playgroud)

但如果我将类型族定义更改为

type family Foo a b :: Bool where
    Foo a a = True
    Foo a b = False
Run Code Online (Sandbox Code Playgroud)

编译得好吗? …

haskell type-families data-kinds

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

在为Reader r编写Applicative实例时如何编写<*>

我坚持在Haskell Book中练习,"第22章读者".练习说"实施阅读器的应用",它给出了以下内容:

{-# LANGUAGE InstanceSigs #-}

newtype Reader r a =
  Reader { runReader :: r -> a }

instance Applicative (Reader r) where 
  pure :: a -> Reader r a
  pure a = Reader $ ???

  (<*>) :: Reader r (a -> b) -> Reader r a -> Reader r b
  (Reader rab) <*> (Reader ra) = Reader $ \r -> ???
Run Code Online (Sandbox Code Playgroud)

pure之后也写了一个Functor实例(我编写了Functor实例,因为否则GHC抱怨"没有(Functor (Reader r)) …实例声明的超类引起的实例在实例声明中为‘Applicative (Reader r)’ …

haskell applicative

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

如何构建自由对象?

所以我理解一个自由对象被定义为一个附属的左侧.但是,如何引导您对Haskell定义此类对象?

更具体地说:给出一个从monad类别到endofunctors类别的"健忘算子",

newtype Forget m a = Forget (m a)
instance Monad m => Functor (Forget m) where
    fmap f (Forget x) = Forget (liftM f x)
Run Code Online (Sandbox Code Playgroud)

然后免费monad Free :: (* -> *) -> (* -> *)是一个类型,承认(一个Monad实例和)以下同构:

type f ~> g = forall x. f x -> g x

fwd :: (Functor f, Monad m) => (f ~> Forget m) -> (Free f ~> m)
bwd :: (Functor f, Monad m) => (Free f ~> m) …
Run Code Online (Sandbox Code Playgroud)

monads haskell free-monad

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