小编mod*_*lar的帖子

Functor用于(a - > b) - >(fa - > fb),什么是(类别c)=> cab - > c(fa)(fb)?

我想有一个函数,用于将纯函数映射到容器或通过它对应用/ monadic动作进行排序.对于纯映射,我们有

fmap :: Functor f => (a -> b) -> (f a -> f b)
Run Code Online (Sandbox Code Playgroud)

对于monadic测序,我们有(来自Data.Taversable)

mapM :: (Traversable f, Monad m) => (a -> m b) -> (f a -> m (f b))
Run Code Online (Sandbox Code Playgroud)

这是类似的

mapKleisli :: (Traversable f, Monad m) => Kleisli m a b -> Kleisli m (f a) (f b)
mapKleisli = Kleisli . mapM . runKleisli
Run Code Online (Sandbox Code Playgroud)

我们知道( - >)和(Kleisli m)都是类别(和箭头).所以自然要做出概括:

mapCategory :: (X f, Category c) => c a b -> c (f a) (f …
Run Code Online (Sandbox Code Playgroud)

haskell category-theory

15
推荐指数
1
解决办法
790
查看次数

如何使用实例组织Haskell模块:坚持数据类型与类型类?

一般的问题是在为现有对象添加实例时哪个模块结构更方便?有哪些优点和缺点?

假设我想为Seq类型添加NFData实例.我可以把它放在:

  • Data.Sequence.Extra(在vty包中做同样的事情)
  • Data.Sequence.Instances.NFData (更确切)
  • Control.DeepSeq.Instances
  • Control.DeepSeq.Instances.Sequence

当我既没有类型类也没有数据类型时就是这种情况.另一种常见的情况是,我拥有一个类型类型,并希望从Hackage的一些"重"包中添加数据类型的实例,比如OpenGL.假设我设计的类型类很轻,与OpenGL没有直接关系.我不希望我的类型类依赖于"重"包,所以我想将OpenGL实例放在一个单独的模块中(这是我的直觉,如果你有其他意见,我们来讨论它).那么,这个模块应该是什么:

  • MyClass.Instances.OpenGL
  • Graphics.Rendering.OpenGL.Extra (连同其他课程的实例)
  • Graphics.Rendering.OpenGL.Instances.MyClass

什么是更灵活的解决方案?在某些时候,OpenGL可以替换为其他库,或者也可以替换MyClass.有微妙的细微差别吗?

此外,如果选择MyClass.Instances变体,哪个方案更好:

  • MyClass.Class类本身的模块和基本实例和MyClass模块重新导出它(也许MyClass.Instances)
  • MyClass类和基本实例的模块,并重新MyClass.All导出所有内容
  • MyClass 类和基本实例的模块,没有用于重新导出的模块.

haskell module instance hierarchy typeclass

13
推荐指数
1
解决办法
1473
查看次数

无法在emacsclient中更改光标颜色

如果我运行emacs --daemon(在.xinitrc或之后)然后emacsclient -c,新创建的帧具有黑色光标颜色,无论是colortheme还是任何其他设置.更确切地说,在我输入任何内容之前,光标颜色是白色(或其他颜色),但是第一个按键时它变为黑色并且无法通过改变set-cursor-color.默认和我的自定义colorthemes都是黑色,所以它使编辑非常不舒服.

如果我跑emacs,M-x server-start而不是emacs --daemon那么这个问题不会出现.但是这种方式我需要保留一个emacs"主"帧而不是偶然杀死它,这似乎不是一个很好的解决方案.

我有以下阻止,.emacs.d/init.el但它没有帮助:

(set-cursor-color "red")
(setq initial-frame-alist '((cursor-color . "red")))
(setq default-frame-alist '((cursor-color . "red")))
(add-hook 'window-setup-hook '(lambda () (set-cursor-color "red")))
(add-hook 'after-make-frame-functions '(lambda (f) (with-selected-frame f (set-cursor-color "red"))))
Run Code Online (Sandbox Code Playgroud)

如果我跑,M-x describe-face RET cursor我得到:

(...)
Defined in `faces.el'.

        Family: unspecified
       Foundry: unspecified
         Width: unspecified
        Height: unspecified
        Weight: unspecified
         Slant: unspecified
    Foreground: unspecified
    Background: black
     Underline: unspecified
      Overline: unspecified
Strike-through: …
Run Code Online (Sandbox Code Playgroud)

emacs emacs-faces

13
推荐指数
3
解决办法
2066
查看次数

在Haskell中编程时如何纠正我的OOP倾向

在Haskell中编程时,我遇到了这个经常出现的问题.在某些时候,我尝试模拟OOP方法.在这里,我为我发现的flash游戏编写了一些AI,我想将各个部分和级别描述为一个部分列表.

module Main where

type Dimension = (Int, Int)
type Position = (Int, Int)
data Orientation = OrienLeft | OrienRight

data Pipe = Vertical | Horizontal | UpLeft | UpRight | DownLeft | DownRight
data Tank = Tank Dimension Orientation
data Bowl = Bowl Dimension
data Cross = Cross
data Source = Source Dimension

-- desired
-- data Piece = Pipe | Tank | Bowl | Cross | Source

-- So that I can put them in a list, and …
Run Code Online (Sandbox Code Playgroud)

oop haskell functional-programming

13
推荐指数
2
解决办法
670
查看次数

在所有类型族实例上键入约束

我想如果没有模板Haskell,我想要的是不可能的,但无论如何我都会问.

我有一个类似Data.Set和类型的接口Data.IntSet:

type family Elem s :: *
class SetLike s where
  insert :: Elem s -> s -> s
  member :: Elem s -> s -> Bool
  ...

type instance Elem (Set a) = a
instance Ord a => SetLike (Set a) where
  ...
Run Code Online (Sandbox Code Playgroud)

我有一个类型系列,它选择最佳集合实现:

type family EfficientSet elem :: *
type instance EfficientSet Int = IntSet
type instance EfficientSet String = Set String -- or another implementation
Run Code Online (Sandbox Code Playgroud)

有没有办法保证EfficientSet实例永远都是SetLike这样Elem (EfficientSet …

haskell types type-families

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

GLU NURBS错误地渲染

编辑:这是一个库bug.我把它报告给HOpenGL邮件列表.

我使用9点矩形方法将圆/椭圆表示为NURBS.

要点是p1, p2, ..., p9,p9 = p1.它们如图所示:

y2 | p2 p3 p4
y1 | p1    p5
y0 | p8 p7 p6
-------------
   | x0 x1 x2

x1 = (x0 + x2) / 2
y1 = (y0 + y2) / 2
Run Code Online (Sandbox Code Playgroud)

p1 = (x0, y1), p2 = (x0, y2)等等.

重量是:

  • 1中间点(p1,p3,p5,p7)
  • sqrt(0.5)角点(p2,p4,p6,p8)

我使用两种方法将权重应用为齐次坐标:

  • 对 - (x,y)重量w变成了Vertex4 (w*x) (w*y) 0 w
  • 错了 - …

opengl haskell nurbs glu

7
推荐指数
1
解决办法
1093
查看次数

如何强制GHC只评估一次静态表达式

join对于有序列表,我有一个类似于SQL的简单例子:if outer参数是True它的联合; 否则它的交集:

import System.Environment

main = do
  [arg] <- getArgs
  let outer = arg == "outer"
  print $ length $ joinLists outer [1..1000] [1,3..1000]

joinLists :: (Ord a, Num a) => Bool -> [a] -> [a] -> [a]
joinLists outer xs ys = go xs ys
  where
  go [] _ = []
  go _ [] = []
  go xs@(x:xs') ys@(y:ys') = case compare x y of
    LT -> append x $ go xs' ys
    GT …
Run Code Online (Sandbox Code Playgroud)

optimization haskell

7
推荐指数
1
解决办法
119
查看次数

polyTypeOf很神秘

PolyTypeableTypeable的类比,用于多态类型.但它的作用相当不可预测:

ghci> :t show
show :: Show a => a -> String
ghci> polyTypeOf show
a1 -> [Char]
ghci> :t fromEnum 
fromEnum :: Enum a => a -> Int
ghci> polyTypeOf fromEnum 

<interactive>:1:12:
    Ambiguous type variable `a0' in the constraint:
      (Enum a0) arising from a use of `fromEnum'
    Probable fix: add a type signature that fixes these type variable(s)
    In the first argument of `polyTypeOf', namely `fromEnum'
    In the expression: polyTypeOf fromEnum
    In an equation for `it': …
Run Code Online (Sandbox Code Playgroud)

polymorphism haskell

6
推荐指数
1
解决办法
145
查看次数

提高基于线路的管道性能的方法

我使用haskell进行基于行的数据处理,即可以应用的任务sed,awk以及类似的工具.作为一个简单的例子,让我们000从标准输入前面加上每一行.

我有三种替代方法来完成任务:

  1. 惰性IO与懒惰ByteStrings
  2. 基于行的管道.
  3. 基于块的管道,ByteString内部采用纯粹的严格处理.

example.hs:

{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}

import ClassyPrelude.Conduit
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as BL8
import qualified Data.Conduit.Binary as CB

main = do
  [arg] <- getArgs
  case arg of

    "lazy" -> BL8.getContents >>= BL8.putStr . BL8.unlines . map ("000" ++) . BL8.lines

    "lines" -> runConduitRes $ stdinC .| CB.lines .|
      mapC ("000" ++) .| …
Run Code Online (Sandbox Code Playgroud)

io haskell conduit

6
推荐指数
1
解决办法
298
查看次数

动态加载Haskell抽象语法表达式

我们可以使用GHC API或其他东西加载非文本源模块,但AST表达式,类似于haskell-src-exts Exp类型?这样我们就可以节省代码生成和解析的时间.

haskell dynamic-loading ghc-api

5
推荐指数
1
解决办法
192
查看次数

模拟"<<%〜"不需要Monoid进行遍历

我需要这样的函数<<%~,其将与行事Traversal以类似的方式s到^?,像这样:

(<<?%~) :: Traversal s t a b -> (a -> b) -> s -> (Maybe a, t)

> ix 0 <<?%~ succ $ [1,2]
(Just 1,[2,2])
> ix 1 <<?%~ succ $ [1,2]
(Just 2,[1,3])
> ix 2 <<?%~ succ $ [1,2]
(Nothing,[1,2])
Run Code Online (Sandbox Code Playgroud)

我该如何实施呢?最显而易见的方法是应用^?%~分开,但我想一气呵成的解决方案.

haskell haskell-lens

5
推荐指数
1
解决办法
51
查看次数