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...
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
...
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?
...
How do I rotate a JPEG image by 45° and save it back to disk?
...
Is it possible to have guards on lambda functions?
For example:
\k
| k < 0 -> "negative"
| k == 0 -> "zero"
| otherwise -> "positive"
...
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...
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 ...
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 ...
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...
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?
...
Hello,
What in Haskell differs from Int ant Integer? In what documentation can i find such things?
Thank you
...
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...
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.
...
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...
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 ...
Why is the function for lifting a value into a functor named pure in Control.Applicative?
...
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...
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.
...
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?
...
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...