Quantcast
Channel: User hammar - Stack Overflow
Browsing latest articles
Browse All 38 View Live

Comment by hammar on How to `derive makeData` a `newType`?

You can use the -XStandaloneDeriving extension to provide the deriving clauses separately without changing the library, e.g. deriving instance Data WriterOptions. Using the Template Haskell-generated...

View Article



Comment by hammar on How does this list comprehension over the inits of...

Another alternative: xs = 1 : scanl1 (+) (map (^2) xs)

View Article

Comment by hammar on Haskell: check if string is valid number

Note that this approach will also accept negative numbers and numbers in scientific format, e.g. "1.3e5".

View Article

Comment by hammar on recommended way to convert Double -> Float in Haskell

@leftaroundabout: Found them. They are implemented as rewrite rules, in this case to GHC.Float.float2Double, which is just a wrapper around the float2Double# primitive.

View Article

Comment by hammar on Why does `peek` with a polymorphic Ptr return...

Note that gsl_rng_type here is just a type variable, so p'gsl_rng_mt19937 has the type Ptr (Ptr a) and your attempt is the same as saying x <- (peek ...) :: IO (Ptr a).

View Article


Comment by hammar on Decrementing ranges in Haskell

Note that this produces an infinite list if a = b.

View Article

Comment by hammar on What is Weak Head Normal Form?

@mucaho: Indeed I did. Good catch!

View Article

Comment by hammar on how to handle capital case in JSON?

@hotGopher: Yes, except for the >>= print part, since decode is a pure function. print (decode "{\"Foo\":\"TEXT\",\"bar\":\"x\"}" :: Maybe Person) should work, though, assuming you have...

View Article


Comment by hammar on RSA function generates public key (e) always to 17

That's just the exponent. The modulus should be different.

View Article


Comment by hammar on repeatedly applying a function until the result is stable

@Caridorc Let's say the list is [1, 2, 3, 4]. With the pattern (x:y:ys) you get ys = [3, 4]. With (x:ys@(y:_)) you get ys = [2, 3, 4]. The ys@(y:_) part is called an "as-pattern", and it binds the...

View Article

Comment by hammar on Sum of numbers under 10,000 that are multiples of 3, 5...

@gia: To generalize this for q factors, you start by adding the multiples of each individual factor, then subtract the multiples of each pair of factors, then add back in each triple, subtract each...

View Article

Answer by hammar for Haskell +"Remove a parameter"

You can use the <*> operator for the (->) instance of Applicative.palindrome = (==) <*> reverseThe <*> operator is kind of like function application in some context. In this case,...

View Article

Answer by hammar for Getting type error in function haskell

Function application has higher precedence than any operator, soputStrLn "No such item - "++ itemmeans(putStrLn "No such item - ") ++ itemYou need to writeputStrLn ("No such item - "++ item)or use the...

View Article


Answer by hammar for Fastest way to get the last element of a list in Haskell

last and init will do the job just fine for a one-off. However they are both O(n), so if you need to manipulate both ends of a list often, as you seem to imply, you might want to consider using...

View Article

Answer by hammar for Simple arithmetic in Haskell

The problem here is that (deducing from the error message) priceOf returns an Int while 0.95 * 8 * 2 has the polymorphic type Fractional a => a, and shouldBe is forcing these types to be the same....

View Article


Answer by hammar for Haskell: check if string is valid number

Here's a simple strategy:Strip off all the digits at the beginning of the string.The remaining string should now be eithera) the empty string, orb) a decimal point followed by all digits.Well, almost....

View Article

Answer by hammar for How to get a particular branch from github

The branch you are trying to check out is in a different repository (a fork), so you'll want to clone that repository and then you can use the -b switch to specify the name of the branch, like this:$...

View Article


Answer by hammar for Regarding Haskell type classes (Num vs Read)

It works because of type defaulting, which causes ambiguous type variables to be defaulted to Integer or Double (or some other user-defined default). This only happens for Num and its subclasses.In...

View Article

Answer by hammar for How do I use newStdGen or getStdGen instead of mkStdGen...

Remember that Haskell functions are pure; they must always return the same result given the same input. You could make your function return IO [a] instead, which would let you call newStdGen, but a...

View Article

Emitting warnings from Template Haskell splices

I know that I can cause a compile-time error by calling fail from a splice, but is it possible to only generate a warning? In particular I would like it to be possible to turn this warning into an...

View Article

Answer by hammar for Haskell / GHCi - loading modules from different directories

You can tell GHCi where to search for modules by using the -i option:ghci Foo.Bar -isrcThis will load src/Foo/Bar.hs into GHCi. This way, you can also specify two different directories like this:ghci...

View Article


Answer by hammar for What's the difference between type and data type in...

The terms are sometimes mixed, but usually a "data type" refers to a type introduced using the data keyword, which has constructors you can pattern match on. These are also called algebraic data...

View Article


Answer by hammar for How to find the frequency of characters in a string in...

The simplest solution is to use a Data.Map to store the intermediate mapping from character to frequency. You can then construct the counts easily using fromListWith. Since Data.Map is sorted, you get...

View Article

Answer by hammar for How to Determine if STDIN is Empty?

I don't think there's a platform independent way of doing this, but on Unix-based systems you should be able to do:#include <unistd.h>...int main() { if (!isatty(0)) { // stdin is being streamed...

View Article

Fun with repeated fmap

I was playing around with functors, and I noticed something interesting:Trivially, id can be instantiated at the type (a -> b) -> a -> b.With the list functor we have fmap :: (a -> b) ->...

View Article


Answer by hammar for Warning: push.default is unset; its implicit value is...

It's explained in great detail in the docs, but I'll try to summarize:matching means git push will push all your local branches to the ones with the same name on the remote. This makes it easy to...

View Article

Answer by hammar for Left and Right Folding over an Infinite list

The key here is laziness. If the function you're using for folding the list is strict, then neither a left fold nor a right fold will terminate, given an infinite list.Prelude> foldr (+) 0...

View Article

Answer by hammar for Composing Database.Esqueleto queries, conditional joins...

Looking at the documentation and the type of select:select :: (...) => SqlQuery a -> SqlPersistT m [r]It's clear that upon calling select, we leave the world of pure composable queries (SqlQuery...

View Article

Answer by hammar for What are practical uses of applicative style?

Since many applicatives are also monads, I feel there's really two sides to this question. Why would I want to use the applicative interface instead of the monadic one when both are available?This is...

View Article



Answer by hammar for Can someone explain the traverse function in Haskell?

I think it's easiest to understand in terms of sequenceA, as traverse can be defined asfollows.traverse :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b)traverse f =...

View Article

Answer by hammar for Understanding GHC assembly output

For top level declarations, it's not too hard. Local definitions can be harder to recognize as their names get mangled and they are likely to get inlined.Let's see what happens when we compile this...

View Article

Answer by hammar for Haskell replace element in list

If you need to update elements at a specific index, lists aren't the most efficient data structure for that. You might want to consider using Seq from Data.Sequence instead, in which case the function...

View Article

Answer by hammar for Laziness/strictness between data and newtype

As you probably know, the main difference between data and newtype is that with data the data constructors are lazy while with newtype the data constructors are strict, i.e. given the following...

View Article


Answer by hammar for Generate a random integer in a range in Haskell without...

A function cannot return an Int without IO, unless it is a pure function, i.e. given the same input you will always get the same output. This means that if you want a random number without IO, you will...

View Article

Answer by hammar for Is it possible to force JavaScript to declare variables...

Yes, it's possible to do this using strict mode. You enable it by putting a statement containing the string literal "use strict" at the top of a file or function to enable strict mode for that...

View Article

Answer by hammar for How to debug Haskell code?

The GHCi option -fbreak-on-exception can be useful. Here's an example debugging session. First we load our file into GHCi.$ ghci Broken.hsGHCi, version 7.0.2: http://www.haskell.org/ghc/ :? for...

View Article


Answer by hammar for What is a regular language?

Here are some of the equivalent definitions from Wikipedia:[...] a regular language is a formal language (i.e., a possiblyinfinite set of finite sequences of symbols from a finite alphabet)that...

View Article


Answer by hammar for What is the time complexity of the sleep sort?

It depends on how those sleeps are implemented. Ultimately they end up in a scheduler somewhere, and the operational complexity will depend on the scheduling algorithm used. For example, if the sleeps...

View Article
Browsing latest articles
Browse All 38 View Live




Latest Images