haskell

Generalizing from a specific type to a class in a GADT

空扰寡人 提交于 2020-01-24 00:41:04
问题 I have the following definitions {-# LANGUAGE GADTs, TypeInType, RankNTypes #-} import Data.Kind class Character (a :: * -> *) where showVal :: a b -> b -> String data ExampleCharacter a where Variable :: ExampleCharacter String EqualSign :: ExampleCharacter () Deref :: ExampleCharacter () instance Character ExampleCharacter where showVal Variable = id showVal EqualSign = const "=" showVal Deref = const "*" data Symbol :: forall a. ExampleCharacter a -> * where Terminal :: a -> Symbol (b ::

XMonad: start program floating based on window title

岁酱吖の 提交于 2020-01-23 21:44:26
问题 I use this truly excellent Firefox add-on: https://github.com/docwhat/itsalltext/ Hit ctrl-e to edit any textarea in Vim. However vim starts up tiled. My browser workspaces are single-window tabbed, so a fullscreen editor is overkill. Here's what I tried to make it start as a floating window: -- This works, but matches any vim instance, not just itsalltext instances: -- , className =? "Gvim" --> doFloat -- This does nothing: , fmap (isInfixOf "itsalltext") title --> doFloat xprop shows this

XMonad: start program floating based on window title

假如想象 提交于 2020-01-23 21:44:10
问题 I use this truly excellent Firefox add-on: https://github.com/docwhat/itsalltext/ Hit ctrl-e to edit any textarea in Vim. However vim starts up tiled. My browser workspaces are single-window tabbed, so a fullscreen editor is overkill. Here's what I tried to make it start as a floating window: -- This works, but matches any vim instance, not just itsalltext instances: -- , className =? "Gvim" --> doFloat -- This does nothing: , fmap (isInfixOf "itsalltext") title --> doFloat xprop shows this

Too many pattern matches to write down for Quadtrees?

北慕城南 提交于 2020-01-23 17:04:38
问题 Imagine a quadtree defined as follow: data (Eq a, Show a) => QT a = C a | Q (QT a) (QT a) (QT a) (QT a) deriving (Eq, Show) bad1 = Q u u u u where u = C 255 bad2 = Q (C 0) (C 255) (Q u u u u) (C 64) where u = C 255 The constructor allows you to create not well-formed quadtrees. bad1 should be simply C 255 and bad2 is not valid too because its bottom-right quadtree (for the same reason, it should be Q (C 0) (C 255) (C 244) (C 64) . So far so good. Checking its well-formness is simply a matter

Too many pattern matches to write down for Quadtrees?

邮差的信 提交于 2020-01-23 17:04:18
问题 Imagine a quadtree defined as follow: data (Eq a, Show a) => QT a = C a | Q (QT a) (QT a) (QT a) (QT a) deriving (Eq, Show) bad1 = Q u u u u where u = C 255 bad2 = Q (C 0) (C 255) (Q u u u u) (C 64) where u = C 255 The constructor allows you to create not well-formed quadtrees. bad1 should be simply C 255 and bad2 is not valid too because its bottom-right quadtree (for the same reason, it should be Q (C 0) (C 255) (C 244) (C 64) . So far so good. Checking its well-formness is simply a matter

Type Variable Location in Transformers

老子叫甜甜 提交于 2020-01-23 17:02:23
问题 Consider the State type - or at least a simplified version: newtype State s a = State { runState :: s -> (a, s) } Now, let's say we want to derive the StateT monad transformer. transformers defines it as follows: newtype StateT s m a = StateT { runStateT :: s -> m (a, s) } Here, the m has been placed on the right of the function arrow, but outside the tuple. However, if we didn't know the correct answer, we might instead put m somewhere else: newtype StateT s m a = StateT { runStateT :: m (s

Optimize Haskell code calculating the sum of all the primes below two million

倖福魔咒の 提交于 2020-01-23 09:20:22
问题 Problem 10 in Project Euler. I saw some discussion there but only for C. I used the following code to calculate: print . sum . sieve $ [2..2000000] where sieve [] = [] sieve (x:xs) = x : sieve (filter ((/= 0) . (`mod` x)) xs) It takes ages to calculate. I am wondering if there is any more efficient way to calculate it? 回答1: Many really fast ways of calculating prime numbers in haskell is described on the haskellwiki page for Prime numbers. Specifically the second one seems to be good enough,

Haskell-way of modeling a type with dynamic JSON fields?

爷,独闯天下 提交于 2020-01-23 08:28:25
问题 I am new to Haskell, coming from an imperative programming background. I would like to be able to serialize an object to JSON in the "Haskell way", but not quite sure how to do that yet. I have read Chapter 5 of RealWorldHaskell which talks about JSON a bit, and played around with Aeson. I have also looked at a few JSON API libraries written in Haskell, such as: Facebook API in Haskell Stripe API in Haskell That got me to the point of being able to create very basic JSON strings from objects

Direct translation of Haskell monad into Scala

不羁岁月 提交于 2020-01-23 07:54:05
问题 Trying to learn how to program monads in Scala, got some troubles Given the quick code sample import Control.Monad newtype LJ a = LJ { session :: a } instance Monad LJ where return s = LJ s (>>=) m f = f ( session m ) instance Functor LJ where fmap f m = LJ . f $ session m type SimpleLJ = LJ String auth :: String -> String -> SimpleLJ auth = undefined readFeed :: String -> SimpleLJ readFeed = undefined closeFeed :: String -> SimpleLJ closeFeed = undefined proceed = auth "123" "456" >>=

“No instance for” error

眉间皱痕 提交于 2020-01-23 06:44:19
问题 Following an example in http://en.wikibooks.org/wiki/Haskell/Beginning Prelude> let abs x = if x < 0 then -x else x Prelude> abs 5 5 Prelude> abs -3 <interactive>:1:6: No instance for (Num (a0 -> a0)) arising from the literal `3' Possible fix: add an instance declaration for (Num (a0 -> a0)) In the second argument of `(-)', namely `3' In the expression: abs - 3 In an equation for `it': it = abs - 3 What's wrong? 回答1: Haskell thinks you're trying to subtract 3 from abs , and is complaining