News aggregator

Python style ternary 'if' in Haskell

Haskell on Reddit - Thu, 07/02/2015 - 12:10pm

I was just playing around when I realised I could do this

infixl 2 if' if' :: a -> Bool -> Maybe a if' a True = Just a if' _ False = Nothing infixr 1 else' else' :: Maybe a -> a -> a else' = flip fromMaybe

This lets you do stuff like

a `if'` test `else'` b a `if'` test `else'` b `if'` test' `else'` c

if' is a useful function on its own.

Yes, it looks pretty ugly with all the quotes and backticks, and the functions could benefit from better names or being operators, but its pretty cool. Thought I'd share.

submitted by 0v3rk1ll
[link] [22 comments]
Categories: Incoming News

Using cabal and stack together

Haskell on Reddit - Thu, 07/02/2015 - 11:47am

From Greg Weber's blog post on stack - - "The good news is that you can build a project with both cabal and stack and the two tools will not conflict."

I've been trying to figure out a clean way of doing that without any success. Tools like ghc-mod only work with cabal and I would like to be able to use them with stack.

My initial assumption was that I can simply get cabal to use the stack package databases by passing --package-db=clear --package-db=<stack snapshot db> --package-db=<stack local db> to cabal configure, but cabal is still not able to find the dependencies already installed by stack.

Is there a clean documented way to achieve this?

submitted by haskman
[link] [9 comments]
Categories: Incoming News

ANNOUNCE: SubHask-0.1

haskell-cafe - Wed, 07/01/2015 - 5:49pm
SubHask is an alternative prelude for programming in subcategories of Hask. The documentation includes examples on: * the category of polynomials * how to give set a monad instance in two different categories * basic linear algebra using the category of linear functions More information at: I'd like to emphasize that SubHask still has a number of bugs that need to be fixed. This release should be seen as just a technology preview.
Categories: Offsite Discussion

Deadline extension: ICFP 2015 Student Research Competition

General haskell list - Wed, 07/01/2015 - 3:12pm
====================================================================== DEADLINE EXTENSION New deadline: 6th July 2015 CALL FOR SUBMISSIONS SRC< at >ICFP 2015 Vancouver, Canada 31 August - 2 September 2015 Co-located with the International Conference on Functional Programming (ICFP 2015) ====================================================================== *** The deadline has been extended by a week! We particularly encourage submissions from undergraduates: none had been received in this category by the original deadline. Note that there is some funding available (up to $500), see the ACM website at *** Student Research Competition ---------------------------- This year ICFP will host a Student Research Competition where undergraduat
Categories: Incoming News

A datatype with a single constructor and forall to multiple constructors without forall

haskell-cafe - Wed, 07/01/2015 - 3:10pm
Hi, Occassionally, in (*good*) haskell libraries, I came across data types that are defined with a single constructor and a forall. For instance, Iteratee[0] is defined as below *newtype Iteratee s m a =* * Iteratee {runIter :: forall r. (a -> Stream s -> m r) -> ((Stream s -> Iteratee s m a) -> Maybe SomeException -> m r) -> m r}* I *think* it is equivalent to the following. But am not sure. *data Iteratee s m a = IOne (m (a , Stream s))* * | ITwo (Stream s -> m (Iteratee s m a)) (Maybe SomeException)* Can some one please explain what are the advantages of using the forall version? Does it significantly improve performance because it is newtype? Or does it help in reasoning about code (in some proof tools or such)? And can some one kindly point me to some references that help me convert from one form to another reliably? Many Thanks, Hemanth _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Small Crash-Course for Haskell

Haskell on Reddit - Wed, 07/01/2015 - 11:55am

Hey all,

About a year ago a few friends of mind were interested in learning Haskell, so I put together a few blog posts to serve as a "crash-course". I've only now gotten around to putting them online, but they cover the basics of syntax, using cabal, typeclasses and Prelude typeclasses, and some of the more abstract ones as well. These won't really be great for everyone since I wrote them with a few specific people in mind, but perhaps someone will enjoy them!

Intro to Haskell Syntax

Intro to Cabal/Gloss



Finger Trees -- this one is an older one, maybe even has been on this sub before.

If you find any errors (logical or formatting), please let me know!

submitted by NiftyIon
[link] [10 comments]
Categories: Incoming News

Use verbs or adjectives/properties to name functions?

Haskell on Reddit - Wed, 07/01/2015 - 9:51am

Considering Haskell uses call-by-need evaluation, you are never really applying a function to anything. All your code describes what things are and not how they are computed.

For example,

map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs

The name "map" acts like a verb, as if the code were "mapping" the function over a list. But, actually, the code is just describing how a new list of "b" can be produced out of a list of "a" and a function "a -> b". Would a name like "mappedBy" make more sense?

mappedBy :: [a] -> (a -> b) -> [b] [] `mappedBy` _ = [] (x:xs) `mappedBy` f = f x : xs `mappedBy` f

I see Haskell library uses verbs and adjectives (or "properties") to name functions. There is no standard or "correct" way to name stuff?

Other examples:

insert :: Ord a => a -> [a] -> [a] -- verb style insert x xs = ... with :: Ord a => [a] -> a -> [a] -- adjective/property style xs `with` x = ... length :: [a] -> Int -- adjective/property style length xs = ... getLength :: [a] -> Int -- verb style getLength xs = ... sort :: Ord a => [a] -> [a] -- verb style sort xs = ... sorted :: Ord a => [a] -> [a] -- adjective/property style sorted xs = ... sortBy :: (a -> a -> Ordering) -> [a] -> [a] -- verb style sortBy f xs = ... sortedBy :: [a] -> (a -> a -> Ordering) -> [a] -- adjective/property style xs `sortedBy` f = ... on :: (b -> b -> c) -> (a -> b) -> a -> a -> c -- adjective/property style f `on` g = ... applyBeforeAndThen :: (a -> b) -> (b -> b -> c) -> a -> a -> c -- verb style applyBeforeAndThen g f = ... submitted by evohunz
[link] [8 comments]
Categories: Incoming News

Mark Jason Dominus: The annoying boxes puzzle

Planet Haskell - Wed, 07/01/2015 - 9:13am
Here is a logic puzzle. I will present the solution on Friday.

There are two boxes on a table, one red and one green. One contains a treasure. The red box is labelled "exactly one of the labels is true". The green box is labelled "the treasure is in this box."

Can you figure out which box contains the treasure?

The treasure is in the red box
The treasure is in the green box
There is not enough information to determine the answer
Something else:
Starting on 2015-07-03, the solution will be here.
Categories: Offsite Blogs

Why does `sort` in Data.Discrimination claim to be O(n)?

Haskell on Reddit - Wed, 07/01/2015 - 9:04am

The documentation for Data.Discrimination has:

sort :: Sorting a => [a] -> [a] O(n). Sort a list using discrimination.

What's the trick?

submitted by clrnd
[link] [19 comments]
Categories: Incoming News

Container or Nix configuration with latest and greatest GHC, emacs, and emacs packages?

Haskell on Reddit - Wed, 07/01/2015 - 8:31am

Hey guys,

I'll get to the point. I use emacs. I use this guide to set up emacs. Things had been working pretty seamlessly for the past few months. Then they stopped working so great. I started getting weird errors. Whereas it used to look like this now it looks like this.

I don't want to have to mess with this. I've contemplated just uninstalling everything, reinstalling haskell-platform, recompiling GHC, reinstalling emacs, downloading all of the emacs packages, etc.

Problem: I don't want to have to do this shit every time emacs messes up on my 3 different devices I work on.

Solution: I'm going to write a bash/python script that does all of it for me.

Question: Has this already been solved? Is there like, a container or Nix config file that does all of this for me already? It would be great if so.

submitted by socratesthefoolish
[link] [4 comments]
Categories: Incoming News

Web Scraping with Lenses

Haskell on Reddit - Wed, 07/01/2015 - 8:23am
Categories: Incoming News

Servant + Persistent With Simple Authorization Checks?

Haskell on Reddit - Wed, 07/01/2015 - 7:28am

/u/bartavelle's recent post on Servant + Persistent (not linking as it seems to tripping up mod filters) and the associated use case is exactly what I am battling with right now. However, most of the article went way above my head so probably won't be attempting that type of solution.

I am dealing with something mundane. a request looks like this:

/user/id/post/id -- get, update, etc.

I do two things: (1) Does the auth token header match the session for the user-id (2) does user-id own post-id.

I return a boolean if it fails. Without worrying about this failure, I specify all my business logic, which can also fail so will return a Maybe at the end. I finally check for the failure pair: if it is (Failed, _) -> I return an unauthorized http status. If it is (_, Failed) -> I return business logic failure response. Otherwise, I return the expected response JSON. I am counting on Haskell's laziness to not evaluate the business logic as any first failure can ignore evaluating the business logic (probably risky?)

This means at the top of all my handlers, I have 2-3 lines of authorization checks. My beginner Haskell brain felt, it will be nicer if there was a combinator in servant, that will take a function (similar params as the handler and return Accept / Reject). This way we can create something like

"user" :> Capture "uid" UserId :> Header "Auth-Token" Text :> Get '[JSON] User :>> MyAcceptRejectFuncType UserId Text

Servant can then first call myAcceptRejectFunc with uid and auth-token and if it says Accept, then it will invoke the regular handler with all the params. Otherwise it will just cut short the request to the client.

Of course, this also leads to some boiler plate because you will want to verify user-id and comment-id too so you will have 2 separate functions verifying ownership. Although, this can probably only be generalized by the app so may not be something servant should worry about.

Is it possible to specify such a chaining logic in servant in a clean manner? Is it even a good idea?

submitted by lleksah15
[link] [5 comments]
Categories: Incoming News

Survey: Which Haskell development tools are you using that make you a more productive Haskell programmer ?

Haskell on Reddit - Wed, 07/01/2015 - 6:59am

1) Are you using Leksah, ghc-mod, hasktags, hoogle, vim/emacs/atom/sublime (plugins)/FPComplete online IDE or something else ?

2) How useful do you think the tools you are using are?

3) What are they useful for you?

4) What problems do they help to solve for you ?

The goal of this question is to get an idea about the current status of Haskell development environment tools, their popularity and percieved usefulness.

This question is NOT about package management/cabal hell. It is purely about writing and reading Haskell code. So to keep the tread tidy and focused please do not comment on package managment and similar tools like nix, halcyon, docker, cabal sandboxes, etc.

I have personally tried Leksah and haskell-vim-now, both seem to be pretty helpful, especially when reading some else's code. The possibility to jump to definition of a given identifier is very handy.

I have also tried sublime,atom,intellij plugins for haskell , respectively and FPComplete online IDE. They did not seem to be as powerful IMHO as Leksah or haskell-vim-now.

I have not tried Emacs.


Emacs + Haskell specific plugins : 14

Vim + Haskell specific plugins : 3.5

Selected quotes:

hdevtools :

"I like the speed of hdevtools but I do not like that type inspection doesn't work on source files with errors. That makes it difficult to use holes productively."

ghc-mod :

"ghc-mod is vital, working without it feels like having a hand tied behind my back. Seeing type errors without the need to compile and being able to inspect types under the cursor is vastly more efficient."

"Also is it just me, or does ghc-mod basically always fail to guess the type?"


"ghci-ng and haskell-mode with proper goto-definition, get any subexpr type, quick type-checking and jump to error, and more are a life-safer for me."

submitted by jocomoco
[link] [144 comments]
Categories: Incoming News

Philip Wadler: The Thrilling Adventures of Lovelace and Babbage

Planet Haskell - Wed, 07/01/2015 - 4:29am
Sydney Padua explores an alternate universe wherein Ada Lovelace and Charles Babbage complete the Analytical Engine and use it to (at the order of Queen Victoria) fight crime. I've blogged before about the web comic, but the book is even better.

Padua's tome reconciles hilarity with accuracy. I am not normally a fan of footnotes: if it is worth saying, say it inline; don't force your poor reader to break the flow of thought and eye, and leap to the bottom of the page. But here is the glorious exception, where the footnotes supplement, argue with, and occasionally threaten to overflow the comic. Even the footnotes have footnotes: endnotes cite sources for the dialogue, present pocket biographies of Ada and Charles' contemporaries Isambard Kingdom Brunel, Charles Dodgson, and George Boole, quote at length from original sources, and explain the workings of the Analytic Engine. In the midst of an illustrated fantasia riff on Alice in Wonderland, the footnotes pursue an academic war as to whether or not Babbage truly considered Lovelace to be the Empress of Number. Padua makes pursuit of Victorian arcana a thrilling adventure of its own. Highly recommended!

Categories: Offsite Blogs

How do you compute a histogram in a pure language?

Haskell on Reddit - Wed, 07/01/2015 - 4:23am

I found this interesting question on Yossi Kreinin's blog: how do you compute a histogram in a pure language? More concretely, you're given an integer n and an array (or list) of integers less than n, and you need to return another array (or list) of length n, where the kth element is the number of elements in the original array which are equal to k.

The simplest implementation in an imperative language would look like this:

for (int i = 0; i < array.length; i++) histogram[array[i]]++;

On the other hand, in a pure language the simplest implementation would probably use a map or functional array, with logarithmic update time.

I'm aware of things like linear types, ST, IORefs, etc. But it would be interesting to have a solution that matched the imperative version in complexity while being pure "in spirit", not just "in letter". Does such a solution exist?

EDIT: the most relevant answer was given by /u/edwardkmett in this subthread. It looks like the problem is open, i.e. there's no known algorithm that fits my bill, but no proof that it's impossible either.

submitted by want_to_want
[link] [124 comments]
Categories: Incoming News