Removing the usage of fromJust

Say, I have some code like this: data Record = Record { validate :: Maybe Bool, mobile :: Text } someFunction :: Monad m => m () someFunction = do (record :: Maybe Record) <- getRecord let hasValidated = join $ validate <$> reco...
more »

2017-02-22 17:02 (4) Answers

Is it possible to have sub-modules in Haskell?

I have this code module M where a = 1 b = 2 x = 10 y = 20 ...but as the module grows it's difficult to deal with duplicate names. Is it possible to have namespaces like this? module M where module A where a = 1 b = 2 module X where...
more »

2017-02-22 12:02 (3) Answers

can I make a list of types in haskell?

I have different data types that I have defined, and I want to make them all an instance of a class. is it possible to make a list of types and map instance over them instead of having to declare them all individually? I mean something like this: d...
more »

2017-02-22 00:02 (1) Answers

How to use the Select monad to solve n-queens?

I'm trying to understand how the Select monad works. Apparently, it is a cousin of Cont and it can be used for backtracking search. I have this list-based solution to the n-queens problem: -- All the ways of extracting an element from a list. oneOf...
more »

2017-02-21 22:02 (2) Answers

Function Type Restrictions

Is it generally preferable to have the strictest or loosest type definition for a function? What are the pros and cons of each approach? I found that when I rewrote my pearson correlation code using strictly doubles, it was easier for me to write, ...
more »

2017-02-21 21:02 (1) Answers

How to mention different modules

I do not know how to import two different modules at the same time. THe following code import qualified Data.Map as Map import Data.List phoneBookToList :: (Ord k) => [(k,String)] -> Map.Map k String phoneBookToList xs = Map.fromListWith (\n...
more »

2017-02-21 16:02 (1) Answers

Why (Num a, Ord a) => a

I have following type signature: let kessel :: (Ord a, Num b) => a -> b -> a; kessel = undefined When I asked for result type: :t kessel 1 2 I've got: kessel 1 2 :: (Num a, Ord a) => a Why not Integer? ...
more »

2017-02-21 12:02 (1) Answers

Why the type is Char -> Char -> Char

I have following type signature: *Main Lib> let f :: a -> a -> a -> a; f = undefined *Main Lib> let x :: Char; x = undefined so to find out, which result type would I get, I did: *Main Lib> :t f x f x :: Char -> Char ->...
more »

2017-02-21 11:02 (2) Answers

What is the lambda in this foldr doing?

I'm working my way through the Haskell book and I'm a bit stumped understanding folds. One exercise asks the reader to write an elem function with foldr. I couldn't get my head round it and googled an example, which I'm now trying to understand. The ...
more »

2017-02-20 23:02 (1) Answers

Empty memory profile

I wanted to test the memory usage of a small test program. The program looks as follows: import Data.List as L main :: IO main = print $ L.find (==100000) [1..1000000000] It finds the 100000th value. I expected to see that this program would only ...
more »

2017-02-20 18:02 (1) Answers

Writing test cases in Haskell for differing types

I've made a library that creates "list like" sequences, and of which many Prelude style functions are implemented. I want to write some test cases for this, to ensure my library is producing correct output, and I thought the easiest way to do this is...
more »

2017-02-20 14:02 (1) Answers

Pattern-matching in Haskell and Erlang

I have a small Erlang function that compares if two lists are equal: myEq([], []) -> true; myEq([X|Xs], [X|Ys]) -> myEq(Xs, Ys); myEq(_, _) -> false. The comparison takes place on line 2, the X of [X|Xs] always binds to ...
more »

2017-02-20 12:02 (2) Answers

How to make illegal values unrepresentable?

A method of design in Functional Programming is making illegal states unrepresentable. I always see this being accomplished with the structure of types, but what about the value of types? What if I have a string called Email and I only want it to ho...
more »

2017-02-20 11:02 (5) Answers

`Show` instance for GHC core

I am trying to work with GHC core data types. I am able to compile my Haskell source to core representation with type Bind CoreBndr. As we know there is no default Show instance for this data type. There is a way to pretty print this representation b...
more »

2017-02-20 11:02 (0) Answers

Allowing agents to have their internal state

I am writing a game in Haskell in which the player and the AI are taking some actions in turns. Until now, the AIs worked by generating actions using all the information about the game, i.e they were functions of the form GameHistory -> GameState ...
more »

2017-02-19 19:02 (1) Answers

Evaluation of higher-order functions

I've written a function getSamplesFromFile that takes a file and returns its contents as a Vector of Floats. The functions reads the contents of the file into a Data.ByteString using Data.ByteString.hGet, it then converts this Data.ByteString to a Ve...
more »

2017-02-19 18:02 (0) Answers