I've been playing around with Haskell a fair bit, including practising writing functions in point-free form. Here is an example function:
dotProduct :: (Num a) => [a] -> [a] -> a
dotProduct xs ys = sum (zipWith (*) xs ys)
I would like to write this function in point-free form. Here is an example I found elsewhere:
dotProduct = (sum ....
Hi, I am a C# developer and these days I am trying to learn Haskell from the online book Real World Haskell. From what I have learnt so far, I am quite impressed with the language. However coming from OO side of the world, I always start with thinking in terms of interfaces, classes and type hierarchies. Because of lack of OO in Haskell,...
Haskell is generally referenced as an example of a purely functional language. How can this be justified given the existence of System.IO.Unsafe.unsafePerformIO ?
Edit: I thought with "purely functional" it was meant that it is impossible to introduce impure code into the functional part of the program.
...
Hi
I am trying to verify something for myself about operator and function precedence in Haskell. For instance, the following code
list = map foo $ xs
can be rewritten as
list = (map foo) $ (xs)
and will eventually be
list = map foo xs
My question used to be, why the first formulation would not be rewritten as
list = (map fo...
I've recently been learning Haskell, and I noticed that the String type (or [Char]) can be ordered. For example, this is valid:
ghci> "foo" > "bar"
True
ghci> "?<>!" `compare` "[&*}"
LT
How does Haskell order Strings, and when would this functionality be useful?
...
I get an error in ghci when I try to define a new type:
Prelude> data Point = Pt Int Int
<interactive>:1:0: parse error on input `data'
Prelude> let data Point = Pt Int Int
<interactive>:1:4: parse error on input `data'
What am I doing wrong?
...
Say I have a list of numbers from 1 to MAGIC_NUMBER -- Is there a way I can declare this beforehand ?
...
I am trying to load a PNG file, get the uncompressed RGBA bytes, then send them to the gzip or zlib packages.
The pngload package returns image data as a (StorableArray (Int, Int) Word8), and the compression packages take lazy ByteStrings. Therefore, I am attempting to build a (StorableArray (Int, Int) Word8 -> ByteString) function.
So...
Is it possible to implement Haskell typeclasses in C++? If yes, then how?
...
For an assignment, we had to implement something like a very basic sexp parser, such that for input like:
"((a b) ((c d) e) f)"
It would return:
[["a", "b"], [["c", "d"], "e"], "f"]
Since this was part of a larger assignment, the parser is only given valid input (matching parens &c). I came up with the following solution in Ruby:
...
I want my showStackHead function take a stack print the head and return the rests, here is my code
--code
showStackHead xx
| xx == [] = return []
| otherwise = do putStrLn("result:" ++ (head xx))
return (tail xx)
when I run this code, the compiler tells me there is a parse...
Unboxed types, like Int#, and strict functions, like f (!x) = ..., are something different, but I see conceptual similarity - they disallow thunks/laziness in some way. If Haskell was a strict language like Ocaml, every function would be strict and every type unboxed. What is the relationship between unboxed types and enforcing strictnes...
In Haskell, why would you define a function with a type constraint:
ghci> :t (==)
(==) :: (Eq a) => a -> a -> Bool
Rather than defining it so it's type was:
ghci> :t (==)
(==) :: Eq -> Eq -> Bool
...
Which are the uses for id function in Haskell?
...
I'm on the I/O chapter of Real World Haskell. Monads aren't discussed in the book for another 7 chapters. Which is to say, my understanding of I/O is, at best, incomplete.
Right now I am trying to comprehend the mapM function. As I understand it, the function "executes" each element in the list which must be an "action" (IO monad).
...
I have a structure called Patch that represents a 2D array of data.
newtype Size = (Int, Int)
data Patch = Patch Size Strict.ByteString
I want to construct a larger Patch from a set of smaller Patches and their assigned positions. (The Patches do not overlap.) The function looks like this:
newtype Position = (Int, Int)
combinePatche...
Not a programming question, but the first time I see something like this.
UNR 1.6 (based on Ubuntu 10.04) installs the 6.12.1 version of GHC.
So, in order to build cabal-install-0.8.2, I have to install
the libghc6- packages of parsec, mtl, network and zlib.
Then, after launching 'sh ./bootstrap.sh', I get:
Checking installed packages ...
I'm wondering why this piece of code doesn't type-check:
{-# LANGUAGE ScopedTypeVariables, Rank2Types, RankNTypes #-}
{-# OPTIONS -fglasgow-exts #-}
module Main where
foo :: [forall a. a]
foo = [1]
ghc complains:
Could not deduce (Num a) from the context ()
arising from the literal `1' at exist5.hs:7:7
Given that:
Prelude> :t ...
And why? There is a bootstrap process and compiling the real compiler process, but which part consumes the most time?
...
I read William Cook's "On Data Abstraction, Revisited", and re-read Ralf Laemmel's "The expression lemma" to try to understand how to apply the former paper's ideas in Haskell. So, I'm trying to understand how could you implement, e.g., a set union function, in Haskell without specifying the types?
...