haskell

Haskell numeric type heirarchy in SICP exercises

I've been learning Haskell recently and was talking to a friend who is working through SICP. We were curious to compare Common Lisp and Scheme and so I decided as an exercise to try to translate exercise 1.29 into Haskell. This exercise uses a function sigma which represents the mathematical Summation function Sigma. This function t...

State monads: trading one pattern for another?

So I'm writing a game in Haskell, and I'm expressing a player's turn as a series of state-altering functions that correlate to various turn phases. Originally, this looks something like: let game' = phase1 game game'' = phase2 game' -- etc. Prime candidate for State monadosity, right? This leads to the more elegant: do phase1 ...

Haskell, Char, and the Turkish I

For the Char data-type, how do I specify that I want to use the Turkish i instead of the English i for the toLower and toUpper functions? ...

Haskell, How do I rotate a JPEG image by 45° and save it back to disk?

How do I rotate a JPEG image by 45° and save it back to disk? ...

Haskell guards on lambda functions?

Is it possible to have guards on lambda functions? For example: \k | k < 0 -> "negative" | k == 0 -> "zero" | otherwise -> "positive" ...

Wadler, "Monads for Functional Programming," Section 2.8

Edit II: Ah, okay: I wasn't understanding how a and b were being bound in the definition of eval! Now I do. If anyone's interested, this is a diagram tracking a and b. I'm a pretty big fan of diagrams. Drawing arrows really improved my Haskell, I swear. A Diagram of an eval call (PDF) Sometimes I feel really dense. In section 2.8 of...

List of custom type and instance of class

Hi, I'm playing around in Haskell to try and get the hang of it. I'm running into problems with my typeclasses. What I'm trying to do is to make a generalized a* module by defining classes and methods and then I'm trying to use those in a program. My problem is that when I try to make a list of my Box data types an instance of my Board ...

Haskell autocompletion in GNU/Emacs

Hello, I installed haskel-mode in emacs. Then I write in my .emacs: (load "~/.emacs.d/haskell-mode/haskell-site-file") (add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode) (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation) (add-hook 'haskell-mode-hook 'haskell-font-lock-symbols t) (put 'downcase-region 'disabled nil) What ...

Improving code to generate a distribution.

Hey, I am new to Haskell and I wonder how/if I can make this code more efficient and tidy. It seems unnecessarily long and untidy. My script generates a list of 10 averages of 10 coin flips. import Data.List import System.Random type Rand a = StdGen -> Maybe (a,StdGen) output = do gen <- newStdGen return $ distBernoulli 10 1...

Haskell Text.Json package can read but not write Rationals?

When I try to decode a JSON file with a floating point number, the Text.JSON package gives me the number as a JSRational. So, I can do a readJSON on a JSRational. However, I can't write rational numbers! Is this on purpose? ...

Haskell Int and Integer

Hello, What in Haskell differs from Int ant Integer? In what documentation can i find such things? Thank you ...

haskell - foldl vs foldr question

I wanted to test foldl vs foldr. From what I've seen you should use foldl over foldr when ever you can due to tail reccursion optimization. This makes sense. However, after running this test I am confused: foldr (takes 0.057s when using time command): a::a -> [a] -> [a] a x = ([x] ++ ) main = putStrLn(show ( sum (foldr a [] [0.. 1000...

Tools for generating Haskell function dependency (control flow) graph?

Note not "functional dependency". Are there tools available that allow me to build a static function dependency graph from source code? Something which indicates to me which functions depend on which other ones in a graphical manner. ...

How does this Haskell function to calculate permutations using list comprehension work?

I'm reading Simon Thompson's Haskell: The Craft of Functional Programming, and I'm wondering how does this work: perms [] = [[]] perms xs = [ x:ps | x <- xs , ps <- perms ( xs\\[x] ) ] I can't seem to grasp how that perms( xs\\[x] ) is supposed to function. The trace of a two element list shows: perms [2,3] [ x:ps | x <- [2,3] , ps...

Explanation of Monad laws

From a gentle introduction to Haskell, there are the following monad laws. Can anyone intuitively explain what they mean? return a >>= k = k a m >>= return = m xs >>= return . f = fmap f xs m >>= (\x -> k x >>= h) = (m >>= k) >>= h Here is my attempted explanation: We expect the return function ...

Naming of `pure` function in Control.Applicative

Why is the function for lifting a value into a functor named pure in Control.Applicative? ...

Zipper like data structure with more then one cursor

The Zipper data structure is great when one wants to traverse a tree and keep the current position, but what data structure one should use if they want to track more then one position? Let me explain with examples: Someone on the #haskell channel has told me that zippers are used in yi editor to represent the cursor position. This is...

Haskell Error: parse error on input `='

Specs GHC 6.12.1 Mac OS X 10.6.4 x64 MacBook Pro Problem I'm having trouble using let syntax. The following code refuses to compile: module Main where main = let x = 1 y = 2 z = 3 in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z I tried tabbing in y = 2 and z = 3 even more. No dice. ...

Reading in a binary file in haskell

How could I write a function with a definition something like... readBinaryFile :: Filename -> IO Data.ByteString I've got the functional parts of Haskell down, but the type system and monads still make my head hurt. Can someone write and explain how that function works to me? ...

How can you change the path where Hugs98 (Haskell) looks for Module and Libraries?

I have installed Ubuntu as a virtual machine so I could use Hugs98. However, after installing I realised I couldn't use Data.Char and Data.Ratio modules. I had to load them manually with :load /usr/lib/etc . It seems this happens because the modules are not in the place where they should be. Can anyone please tell me how I can change the...