I'm trying to implement Steinhaus-Johnson-Trotter algorithm for generating permutations. My code is below: permutations :: [a] -> [[a]] permutations [] = [] permutations (x:[]) = [[x]] permutations xs = [ys ++ [xs !! i] | i <- [len,len-1..0], ys <- permutations (delete i xs)] where len = (length xs) delete i xs = take i xs ++ drop (succ i) xs This is a direct translation
我正试图实施用于生成排列的Steinhaus-Johnson-Trotter算法。 我的代码如下: permutations :: [a] -> [[a]] permutations [] = [] permutations (x:[]) = [[x]] permutations xs = [ys ++ [xs !! i] | i <- [len,len-1..0], ys <- permutations (delete i xs)] where len = (length xs) delete i xs = take i xs ++ drop (succ i) xs 这是Python代码的直接翻译: def perms(A): if len(A)==1:
I have an EDSL which offers list-like combinators for arrays ( map , zipWith , etc..) Some combinators require certain inputs to be random access. Eg the data array of Gather picking the elements from data array at the indices specified by the other: Gather (Manifest [10,11,12]) (Manifest [2,0,0,1,2]) = [12,10,10,11,12] The language makes use of data-reify package for recovering sharing. Th
我有一个EDSL,它提供了数组的列表式组合器( map , zipWith等)。 一些组合器需要某些输入才能随机访问。 例如, Gather的数据数组在另一个指定的索引处从数据数组中挑选元素: Gather (Manifest [10,11,12]) (Manifest [2,0,0,1,2]) = [12,10,10,11,12] 该语言使用data-reify通用包来恢复共享。 问题是有时同一个子树包含需要提供随机访问的节点和可以顺序计算的节点。 让他们共享会破坏随后的评估者。 例如,在下面
I'd like to use latex notation for equations in my source code. For example, I would write the following comment in some haskell source file Equations.hs: -- | $v = frac{dx}{dt}$ In the doc directory, this gets rendered by haddock in Equations.tex as: {char '44}v = frac{char '173}dx{char '175}{char '173}dt{char '175}{char '44} I found this function in the source for Haddock's latex b
我想在我的源代码中使用乳胶符号表达方程式。 例如,我会在一些Haskell源文件Equations.hs中编写以下注释: -- | $v = frac{dx}{dt}$ 在doc目录中,这通过Equations.tex中的haddock呈现为: {char '44}v = frac{char '173}dx{char '175}{char '173}dt{char '175}{char '44} 我在Haddock乳胶后端的源代码中找到了这个函数,它取代了乳胶格式中使用的许多字符: latexMunge :: Char -> String -> String ... latexMunge
Suppose I have a data type like so: data Color = Red | Blue | Green How would I generate a function like this using templatehaskell? myShow Red = ... myShow Blue = ... myShow Green = ... ie I'm looking for multiple definitions for a function based on pattern-matching. {-# LANGUAGE TemplateHaskell #-} module Test where import Language.Haskell.TH data Color = Red | Blue | Green myS
假设我有这样的数据类型: data Color = Red | Blue | Green 我将如何使用templatehaskell生成这样的函数? myShow Red = ... myShow Blue = ... myShow Green = ... 即我正在寻找基于模式匹配功能的多个定义。 {-# LANGUAGE TemplateHaskell #-} module Test where import Language.Haskell.TH data Color = Red | Blue | Green myShow' :: Q [Dec] myShow' = return [FunD (mkName "myShow") [mkClause 'Red, mkCl
OK, so here's an obscure corner of the language: Haskell allows you to export an identifier who's type signature mentions a type that is not exported. What exactly is the semantics of this? For example, suppose I have module Foobar (bar) where data Foo = ... bar :: String -> Foo Foo is not exported, while bar is. The type of bar mentions Foo , though. Many programming langua
好的,所以这里是一个不太明显的语言角落: Haskell允许你导出一个类型签名的标识符,提到一个不导出的类型。 这到底是什么语义? 例如,假设我有 module Foobar (bar) where data Foo = ... bar :: String -> Foo Foo不会导出,而bar是。 不过, bar的类型提到了Foo 。 许多编程语言不会让你这样做,但Haskell会这样做。 所以现在怎么办? 看来我可以打电话给bar ,但结果却不能做得太多。 特别是,我(大概
{-# LANGUAGE GADTs #-} data Foo x y where Composition :: Foo b c -> Foo a b -> Foo a c FMap :: Functor f => (a->b) -> Foo (f a) (f b) asFunction :: Foo a b -> a->b asFunction (FMap m) = fmap m -- asFunction (Composition (FMap m) (FMap n)) = fmap m . fmap n asFunction (Composition m n) = asFunction m . asFunction n This works as expected... until you uncomment the second
{-# LANGUAGE GADTs #-} data Foo x y where Composition :: Foo b c -> Foo a b -> Foo a c FMap :: Functor f => (a->b) -> Foo (f a) (f b) asFunction :: Foo a b -> a->b asFunction (FMap m) = fmap m -- asFunction (Composition (FMap m) (FMap n)) = fmap m . fmap n asFunction (Composition m n) = asFunction m . asFunction n 这按预期工作......直到你取消注释asFunction的第二个子句
Is there any overhead of using Sing from GHC.TypeLits? For example for the program: {-# LANGUAGE DataKinds #-} module Test (test) where import GHC.TypeLits test :: Integer test = fromSing (sing :: Sing 5) GHC generates core code: Test.test1 :: GHC.Integer.Type.Integer [GblId, Str=DmdType, Unf=Unf{Src=<vanilla>, TopLvl=True, Arity=0, Value=True, ConLike=True, WorkFree=True,
从GHC.TypeLits使用Sing有什么开销? 例如对于该程序: {-# LANGUAGE DataKinds #-} module Test (test) where import GHC.TypeLits test :: Integer test = fromSing (sing :: Sing 5) GHC生成核心代码: Test.test1 :: GHC.Integer.Type.Integer [GblId, Str=DmdType, Unf=Unf{Src=<vanilla>, TopLvl=True, Arity=0, Value=True, ConLike=True, WorkFree=True, Expandable=True, Guidance=IF_
I'm working with languages embedded in Haskell. My languages can be printed out as source code, so I created a Compile class and made a class instance for every program element that can be printed out. That way I could dump my code compositionally. This worked fine before the concepts of modes was considered. Each language can be used in two modes (implemented as instances of class Mode
我正在使用Haskell中嵌入的语言。 我的语言可以作为源代码打印出来,所以我创建了一个Compile类并为每个可以打印的程序元素创建一个类实例。 这样我可以在构图上转储我的代码。 在模式的概念被考虑之前,这工作得很好。 每种语言都可以在两种模式下使用(实现为类Mode实例)。 在简单模式下,一切都很正常。 在命名模式下,很多程序元素可以用字符串替换。 (它像宏定义一样工作。) 我想保留所有表示类型安全。 所以
I am doing things with STM and have among other things used the TBQueue data structure with great success. An useful feature I've been using it for involves reading from it based on a precondition in a TVar , basically like so: shouldRead <- readTVar shouldReadVar if shouldRead then do a <- readTBQueue queue doSomethingWith a else doSomethingElse If we assume that queue i
我正在做STM的工作,除此之外还使用TBQueue数据结构取得了巨大成功。 我一直在使用它的一个有用的功能涉及基于TVar的前提条件来读取它,基本上如此: shouldRead <- readTVar shouldReadVar if shouldRead then do a <- readTBQueue queue doSomethingWith a else doSomethingElse 如果我们在执行此块之前假设queue为空且shouldReadVar包含True ,则会导致readTBQueue调用retry ,并且当shouldReadVar包含F
I am trying to teach myself Hindley-Milner type inference by implementing Algorithm W in the language I usually use, Clojure. I am running into an issue with let inference, and I'm not sure if I'm doing something wrong, or if the result I'm expecting requires something outside of the algorithm. Basically, using Haskell notation, if I try to infer the type of this: a -> let b =
我试图通过在我通常使用的语言Clojure中实现算法W来教导自己Hindley-Milner类型推理。 我正在与一个问题let推断,我不知道如果我做错了什么,或者如果我期待的结果,需要在算法之外的东西。 基本上,使用Haskell符号,如果我试图推断这种类型: a -> let b = a in b + 1 我得到这个: Num a => t -> a 但我应该得到这个: Num a => a -> a 再次,我实际上是在Clojure中这样做的,但我不认为这个问题是Cl