haskell

Is it possible to use cmake for Haskell projects?

耗尽温柔 提交于 2020-01-01 04:18:06
问题 I am planning a project written in Haskell, maybe there are some parts in C as well. For the buildsystem I decided against the common choice for Haskell programs cabal, mainly because I want to learn how building programs in other languages works. I heard about CMake and I think it is a pretty cool product. Although I have no knowledge in how to use it, I want to use CMake for that project, just to find out how it works. Googleing did not reveal any facts about how to use cmake with haskell,

What are uses of polymorphic kinds?

青春壹個敷衍的年華 提交于 2020-01-01 04:17:48
问题 Polymorphic kinds are an extension to Haskell's type system, supported by UHC, allowing data A x y = A (y x) to be typed (kinded?) as a -> (a -> *) -> * . What are they useful for? 回答1: One possible usage example can be using conal's TypeCompose for composing monad transformers in point-free style. type MyT = StateT Foo :. MaybeT :. ContT Bar (just as an example, I have no idea what one's going to do with those foos and bars..) Instead of: type MyT m = StateT Foo (MaybeT (ContT Bar m)) (this

Mix and match stateful computations within the State monad

独自空忆成欢 提交于 2020-01-01 04:15:06
问题 The state of my program consists of three values, a , b , and c , of types A , B , and C . Different functions need access to different values. I want to write functions using the State monad so that each function can only access the pieces of the state that it needs to access. I have four functions of the following types: f :: State (A, B, C) x g :: y -> State (A, B) x h :: y -> State (B, C) x i :: y -> State (A, C) x Here is how I call g within f : f = do -- some stuff -- y is bound to an

Get a Haskell record's field names as a list of strings?

半城伤御伤魂 提交于 2020-01-01 04:11:08
问题 Say I have the following: data Rec = Rec { alpha :: Int, beta :: Double, phi :: Float } sample = Rec 1 2.3 4.5 I understand Template Haskell & the reify function can get me the record's field names. That is: print $(f sample) --> ["alpha", "beta", "phi"] There is also a claim that this can be done without Template Haskell. Can someone provide an example implementation for this can be accomplished? 回答1: It can be done with a Data (most GHC versions) or Generic (7.2.x and up) instance, which

Can you formulate the insertion sort as a monoid in Clojure?

旧时模样 提交于 2020-01-01 03:37:15
问题 This is the code for an insertion sort in Clojure: (defn in-sort! [data] (letfn [(insert ([raw x](insert [] raw x)) ([sorted [y & raw] x] (if (nil? y) (conj sorted x) (if (<= x y ) (concat sorted [x,y] raw) (recur (conj sorted y) raw x )))))] (reduce insert [] data))) ;Usage:(in-sort! [6,8,5,9,3,2,1,4,7]) ;Returns: [1 2 3 4 5 6 7 8 9] This is the insertion sort formulated as a monoid in Haskell: newtype OL x = OL [x] instance Ord x => Monoid (OL x) where mempty = OL [] mappend (OL xs) (OL ys)

Listing the paths to leaves in a tree

三世轮回 提交于 2020-01-01 03:29:13
问题 I'm trying to write a function that finds all of the paths to the leaves in a tree. For example, given a tree that looks like this: 1 / \ 2 5 / \ \ 3 4 6 The output list would be: [[1,2,3],[1,2,4],[1,5,6]] . The type signature for this function is: branches :: Tree a -> [[a]] . Note that this uses the Tree type defined in the Data.Tree package, and that although the example tree is binary, the actual tree type is a rose tree. 回答1: I would build the paths by adding new labels to the front of

Experience reports using indexed monads in production?

萝らか妹 提交于 2020-01-01 02:48:06
问题 In a previous question I discovered the existence of Conor McBride's Kleisli arrows of Outrageous Fortune while looking for ways of encoding Idris examples in Haskell. My efforts to understand McBride's code and make it compile in Haskell led to this gist: https://gist.github.com/abailly/02dcc04b23d4c607f33dca20021bcd2f While searching on Hackage, I discovered several implementations of these concepts, notably by (guess who?) Edward Kmett and Gabriel Gonzalez. What experience do people have

How should the general type of a “lemma” function be understood?

烈酒焚心 提交于 2020-01-01 02:39:10
问题 Perhaps this is a stupid question. Here's a quote from the Hasochism paper: One approach to resolving this issue is to encode lemmas, given by parameterised equations, as Haskell functions. In general, such lemmas may be encoded as functions of type: ∀ x1 ... xn. Natty x1 → ... → Natty xn → ((l ~ r) ⇒ t) → t I thought I understood RankNTypes , but I can't make sense of the last part of this proposition. I'm reading it informally as "given a term which requires l ~ r , return that term". I'm

Examples of Haskell Applicative Transformers

北慕城南 提交于 2020-01-01 02:32:27
问题 The wiki on www.haskell.org tells us the following about Applicative Transformers: So where are applicative transformers? The answer is, that we do not need special transformers for applicative functors since they can be combined in a generic way. http://www.haskell.org/haskellwiki/Applicative_functor#Applicative_transfomers I tried the following in order to try to combine a bunch of applicative functors. But all I got was bunch of errors. Here is the code: import Control.Applicative import

Remove elements by index in haskell

☆樱花仙子☆ 提交于 2020-01-01 02:32:13
问题 I'm new in haskell and I'm looking for some standard functions to work with lists by indexes. My exact problem is that i want to remove 3 elements after every 5. If its not clear enough here is illustration: OOOOOXXXOOOOOXXX... I know how to write huge function with many parameters, but is there any clever way to do this? 回答1: Two completely different approaches You can use List.splitAt together with drop: import Data.List (splitAt) f :: [a] -> [a] f [] = [] f xs = let (h, t) = splitAt 5 xs