Rohit Sharma | 25 Oct 05:31 2014
Picon

Question on syntax

All,

I started learning haskell very recently and have a question on the pattern matching style for lists.

In the below snippet i.e. "(x:xs)" why do we went with round braces and not square? I know we are using cons that tells this is not a tuple but would it not make more sense to write something like [x:xs] instead of (x:xs), i thought round braces was used for pair/tuples?

safeHead [] = Nothing
safeHead (x:xs) = Just x

Thanks,
Rohit
<div><div dir="ltr">All,<div><br></div>
<div>I started learning haskell very recently and have a question on the pattern matching style for lists.<div><br></div>
<div>In the below snippet i.e. "(x:xs)" why do we went with round braces and not square? I know we are using cons that tells this is not a tuple but would it not make more sense to write something like [x:xs] instead of (x:xs), i thought round braces was used for pair/tuples?</div>
<div><br></div>
<div>safeHead [] = Nothing</div>
<div>safeHead (x:xs) = Just x</div>
</div>
<div><br></div>
<div>Thanks,</div>
<div>Rohit</div>
</div></div>
Julius Gedvilas | 24 Oct 15:30 2014
Picon

GHCi error msgs

How difficult would it be to change format of GHCi compile-time error-messages of type "couldn't match expected type"?
<div><div dir="ltr">How difficult would it be to change format of GHCi compile-time error-messages of type "couldn't match expected type"?</div></div>
Dimitri DeFigueiredo | 20 Oct 20:57 2014
Picon

learning lazy evaluation

I found an interesting situation while making recursive calls that I am 
trying to understand. I re-wrote the 'minimum' function from the prelude 
as such:

minimum_bug :: (Ord a) => [a] -> a
minimum_bug  [x] = x
minimum_bug  (x:xs) | x > minimum_bug xs = minimum_bug xs
                     | otherwise          = x

(I understand I should be using something like "minbug xs = foldr1 min 
xs" for this, but bare with me)

The interesting thing is that the function has exponential time 
behavior. In a way, that makes sense as I am making two recursive calls. 
But I was expecting GHC to be smart enough to transform it into 
something like:

minimum_bug' :: (Ord a) => [a] -> a
minimum_bug'  [x] = x
minimum_bug'  (x:xs) | x > y      = y
                      | otherwise  = x
                         where y = minimum_bug' xs

(This one always works as expected)

I understand that lazy evaluation implies memoization in some cases.
When does GHC only use memoization to avoid this kind of behavior?

Another interesting fact is that in my simple tests the exponential 
behavior does NOT occur when I pass the function a list in sorted order.

main = do putStrLn "started - in order list"
           putStrLn $ show $ minimum_bug [1..30000]  -- no problem
           putStrLn "started - out of order list"
           putStrLn $ show $ minimum_bug [27,26..1]  -- don't do this 
with large numbers!
           putStrLn "done!"

It's not clear to me how the sorted list is able to escape the 
exponential slow down.

Cheers,

Dimitri

Jeremy | 20 Oct 11:32 2014
Picon

GhcDynamic and DYNAMIC_GHC_PROGRAMS

mk/config.mk contains GhcDynamic and DYNAMIC_GHC_PROGRAMS variables. What is
the difference between them? From the names and comments, it looks like
DYNAMIC_GHC_PROGRAMS controls whether all the GHC binaries will be static or
dynamically linked, but GhcDynamic only controls the GHC executable?

Jeffrey Brown | 19 Oct 22:13 2014
Picon

Is working with Haskell easier on Linux than on a Mac?

I have read that Haskell is easier to work with on Linux than on Windows. Is Haskell on Linux also easier than Haskell on OS X?

I'm trying to do realtime OSC output from Haskell, because concurrency in Python is hard. Brandon Allbery on the haskell-cafe list, told me "chrt" and "sched_setscheduler" would be helpful. At the shell prompt I found that my system (OS X 10.9) does not recognize "chrt". I found a library on Hackage, "posix-realtime", that claims Mac compatibility and has a "sched_setscheduler" function, but my attempts to install it fail:

sh-3.2# cabal install posix-realtime
Resolving dependencies...
Downloading unix-2.3.2.0...
Configuring unix-2.3.2.0...
Building unix-2.3.2.0...
Failed to install unix-2.3.2.0
Last 10 lines of the build log ( /var/root/.cabal/logs/unix-2.3.2.0.log ):
Building unix-2.3.2.0...
Preprocessing library unix-2.3.2.0...
dist/build/System/Posix/Signals.hs:124:10:
fatal error: 'Signals.h' file not found
#include "Signals.h"
^
1 error generated.

Even if I found a solution to this particular problem, I'm worried I'll keep running into similar ones, because I'm sure I'll keep trying new packages. Would this kind of work be substantially easier if I were using, say, Linux Mint?
<div><div dir="ltr">
<div>I have read that Haskell is easier to work with on Linux than on Windows. Is Haskell on Linux also easier than Haskell on OS X?</div>
<div><br></div>
<div>I'm trying to do realtime OSC output from Haskell, because concurrency in Python is hard. Brandon Allbery on the haskell-cafe list, told me "chrt" and "sched_setscheduler" would be helpful. At the shell prompt I found that my system (OS X 10.9) does not recognize "chrt". I found a library on Hackage, "posix-realtime", that claims Mac compatibility and has a "sched_setscheduler" function, but my attempts to install it fail:</div>
<div><br></div>
<div>
<span class="">	</span>sh-3.2# cabal install posix-realtime</div>
<div>
<span class="">	</span>Resolving dependencies...</div>
<div>
<span class="">	</span>Downloading unix-2.3.2.0...</div>
<div>
<span class="">	</span>Configuring unix-2.3.2.0...</div>
<div>
<span class="">	</span>Building unix-2.3.2.0...</div>
<div>
<span class="">	</span>Failed to install unix-2.3.2.0</div>
<div>
<span class="">	</span>Last 10 lines of the build log ( /var/root/.cabal/logs/unix-2.3.2.0.log ):</div>
<div>
<span class="">	</span>Building unix-2.3.2.0...</div>
<div>
<span class="">	</span>Preprocessing library unix-2.3.2.0...</div>
<div><span class="">	</span></div>
<div>
<span class="">	</span>dist/build/System/Posix/Signals.hs:124:10:</div>
<div>
<span class="">			</span> fatal error: 'Signals.h' file not found</div>
<div>
<span class="">	</span>#include "Signals.h"</div>
<div>
<span class="">					</span> ^</div>
<div>
<span class="">	</span>1 error generated.</div>
<div><br></div>
<div>Even if I found a solution to this particular problem, I'm worried I'll keep running into similar ones, because I'm sure I'll keep trying new packages. Would this kind of work be substantially easier if I were using, say, Linux Mint?</div>
</div></div>
Frank | 19 Oct 00:37 2014
Picon

Functors and Applicatives; I'm just not getting it ...

I've had a go at LYAH and CIS 194 and the Typeclassopedia and I just don't get get functors and applicatives. I'm simply not understanding them, what the various symbols/keywords mean, what they represent, how to think of them, etc. Nothing. Is there any kind of documented model I should be considering? Is there a "functors and applicatives for Dummies" I should read? Should I just give it up, not bother with Haskell and just stick to scheme/ruby/C++?

--
P.S.: I prefer to be reached on BitMessage at BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6
<div><div dir="ltr">I've had a go at LYAH and CIS 194 and the Typeclassopedia and I just don't get get functors and applicatives. I'm simply not understanding them, what the various symbols/keywords mean, what they represent, how to think of them, etc. Nothing. Is there any kind of documented model I should be considering? Is there a "functors and applicatives for Dummies" I should read? Should I just give it up, not bother with Haskell and just stick to scheme/ruby/C++?<br clear="all"><div><br></div>-- <br>P.S.: I prefer to be reached on BitMessage at BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6
</div></div>
Keeley Abbott | 18 Oct 17:08 2014
Picon

Trouble Understanding How to Start

Hi All,

So, I am taking a Functional Programming class this fall, and I have struggling to understand how to build
types and functions correctly. For some reason it just isn’t clicking. I have the following module I am
working on, and I need some guidance.

— Variable names
type Name = String

— An environment for looking up the value of a variable
type Env = Name -> Maybe Int

— An empty environment
empty :: Env
empty env = Nothing

— Set a variable to a value in the environment if it doesn’t already exist
set :: Name -> Int -> Env -> Env
set x i e = \y -> if x == y then (Just i) else e y

— Lookup the value of a variable in the environment
get :: Name -> Env -> Int
get x env = ?? (I previously had “type Env = Name -> Int”, so this was working with env x, but we had to change
empty to be something other than just a runtime error, which is why I made Env = Name -> Maybe Int)

—Remove a variable from the environment
unset :: Name -> Env -> Env
unset x e = \y -> if x == y then empty x else env y

At this point I don’t know how to fix get, so I get an int to display (it keeps telling me it can’t match the
type ‘Maybe Int’ with the expected type ‘Int’). And I don’t know where to go from there to create
getOr, setAll, and mapEnv functions, because I am just not understanding what I am doing. For the most part
I have been doing simpler things that I could just mess with the functions until they work, but even at that
I’m not getting HOW they work… Any assistance or instruction on how I can GET what I am trying to do would
be greatly appreciated.

Thanks,
Kallarhynn
Johannes Joachim Engels | 18 Oct 12:02 2014
Picon

How to override a type class instance ?


Hello,

say, I am not happy with the Show instances of arrays, e.g.

> import Data.Array.Unboxed
> let x = listArray ((0,0), (1,1)) [0.0 .. 3.0] :: UArray (Int,Int) Double
> x
array ((0,0),(1,1)) [((0,0),0.0),((0,1),1.0),((1,0),2.0),((1,1),3.0)]

but I would like to see  something more Matlab-like as

0.000  1.000
2.000  3.000

so I wish to define my own Show instance for UArray (Int,Int) Double. How can I override the "standard" show method for UArray ? I expected something like

> import Data.Array.Unboxed hiding (show)
or
> import Data.Array.Base hiding (show)

should do, but still I get the error message
"Overlapping instances for Show (UArray (Int, Int) Double)",
obviously because my own instance declaration of Show collides with that one in Data.Array.Base. How can I deactivate the latter?

Regards Johannes


<div>
<div>
<br><div>Hello,<br><br>
say, I am not happy with the Show instances of arrays, e.g.<br><br>
&gt; import Data.Array.Unboxed<br>
&gt; let x = listArray ((0,0), (1,1)) [0.0 .. 3.0] :: UArray (Int,Int) Double<br>
&gt; x<br>
array ((0,0),(1,1)) [((0,0),0.0),((0,1),1.0),((1,0),2.0),((1,1),3.0)]<br><br>
but I would like to see&nbsp; something more Matlab-like as<br><br>
0.000&nbsp; 1.000<br>
2.000&nbsp; 3.000<br><br>
so I wish to define my own Show instance for UArray (Int,Int) Double. How can I override the "standard" show method for UArray ? I expected something like<br><br>
&gt; import Data.Array.Unboxed hiding (show)<br>
or <br>
&gt; import Data.Array.Base hiding (show)<br><br>
should do, but still I get the error message <br>
"Overlapping instances for Show (UArray (Int, Int) Double)", <br>
obviously because my own instance declaration of Show collides with that one in Data.Array.Base. How can I deactivate the latter?<br><br>
Regards Johannes<br><br><br>
</div>
</div>
</div>
Dimitri DeFigueiredo | 17 Oct 05:55 2014
Picon

QuickCheck Crazy

Hello Everyone,

I am trying to do homework #3 of Stephanie Weirich's programming class. 
It is about QuickCheck.
http://www.seas.upenn.edu/~cis552/current/hw/hw03/index.html

However, quickCheck is magically coming up with the wrong answer!
I am loading a very simple file (distilled from the homework). Here it is:

~~~~
import Test.QuickCheck

------------------------------------------------------------------------------
prop_const :: Eq a => (a -> a -> a) -> a -> a -> Bool
prop_const const' a b = const' a b == a

const_bug :: a -> b -> b
const_bug _ b = b -- Oops: this returns the *second* argument, not the 
first.

--main = quickCheck (prop_const const)

~~~~
Now, I just wanted to check that 'prop_const' holds for the 'const' 
function, but fails for 'const_bug'
So, I fired up GHCi, but here's what happens:

econ1gw-131-21-dhcp:week3 dimitri$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l weird-quickcheck.hs
[1 of 1] Compiling Main             ( weird-quickcheck.hs, interpreted )

weird-quickcheck.hs:22:1: Warning:
     The import of `Test.QuickCheck' is redundant
       except perhaps to import instances from `Test.QuickCheck'
     To import instances alone, use: import Test.QuickCheck()
Ok, modules loaded: Main.
*Main> const 1 2
1
*Main> const_bug 1 2
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package old-locale-1.0.0.5 ... linking ... done.
Loading package time-1.4.0.1 ... linking ... done.
Loading package random-1.0.1.1 ... linking ... done.
Loading package primitive-0.5.0.1 ... linking ... done.
Loading package pretty-1.1.1.0 ... linking ... done.
Loading package containers-0.5.0.0 ... linking ... done.
Loading package template-haskell ... linking ... done.
Loading package tf-random-0.5 ... linking ... done.
Loading package transformers-0.3.0.0 ... linking ... done.
Loading package QuickCheck-2.7.6 ... linking ... done.
2
*Main> :t prop_const
prop_const :: Eq a => (a -> a -> a) -> a -> a -> Bool
*Main> quickCheck (prop_const const)
+++ OK, passed 100 tests.
*Main> quickCheck (prop_const const_bug)
+++ OK, passed 100 tests.
*Main> quickCheck (prop_const const_bug :: Char -> Char -> Bool)
*** Failed! Falsifiable (after 1 test and 2 shrinks):
'a'
'b'
*Main>

This makes no sense to me!
First, I have no idea how quickCheck is able to run any tests at all as 
the type of 'const' is:
*Main> :t const
const :: a -> b -> a

In other words, it's a polymorphic type.

Second, why does GHCi load so many files when I try to evaluate 'const_bug'?

Finally and most puzzling, why does quickCheck give out the wrong result 
if I don't specify the type when testing 'const_bug'?
That's really scary. It means that quickCheck can easily fool me if I 
forget to specify the necessary types.

(GHC complains if I uncommented the '--main' line, so I can only do this 
in GHCi.)

I am totally lost here, any pointers would be much appreciated.

Thanks,

Dimitri

Michael L Martin | 15 Oct 19:55 2014
Picon

latest Haskell Platform build fails

Hello all,

I am trying to install the latest version of Haskell Platform and am 
having a problem.

I created a new VM and installed Ubuntu 14.04 server and applied the 
latest updates. Also installed packages
"build-essential" and "zlib1g-dev".

I then downloaded and installed ghc-7.8.3 from 
https://www.haskell.org/ghc/download_ghc_7_8_3#x86_64linux
and then downloaded and installed cabal 1.20.0.3 from 
http://hackage.haskell.org/package/cabal-install.

After installing cabal, I did a "cabal update" followed by "cabal 
install hscolour".

Then downloaded the latest Haskell Platform tarball 
(haskell-platform-2014.2.0.0-srcdist.tar.gz) and attempted to
install like this:

./platform.sh ../ghc-7.8.3-x86_64-unknown-linux-deb7.tar.bz2

This fails like this:

***
*** Building hptool
***
Building hptool-0.1...
Preprocessing executable 'hptool' for hptool-0.1...
***
*** Running hptool
***
# rm (for 
build/.markers/rel/build/target/usr/local/haskell/ghc-7.8.3-x86_64/lib/happy-1.19.4)
# rm (for build/.markers/rel/build/package/happy-1.19.4/build)
happy-1.19.4 needs mtl-2.1.3.1
 >>> Building happy-1.19.4
# cp (for build/.markers/rel/build/package/happy-1.19.4/build)
# rm (for build/.markers/rel/build/package/happy-1.19.4/build)
# ghc-pkg (for build/.markers/rel/build/package/happy-1.19.4/build)
# ghc-pkg (for build/.markers/rel/build/package/happy-1.19.4/build)
Reading package info from "build/package/mtl-2.1.3.1/inplace.conf" ... done.
mtl-2.1.3.1: package(s) with this id already exist: mtl-2.1.3.1
Error when running Shake build system:
* build-all
* build-product
* build/product/haskell-platform-2014.2.0.0-unknown-posix-x86_64.tar.gz
* build/target
* 
build/.markers/rel/build/target/usr/local/haskell/ghc-7.8.3-x86_64/lib/happy-1.19.4
* build/.markers/rel/build/package/happy-1.19.4/build
Development.Shake.command, system command failed
Command: 
/home/mmartin/Downloads/haskell-platform-2014.2.0.0/build/ghc-bindist/local/bin/ghc-pkg 
register --package-db=build/package/happy-1.19.4/package.conf.d 
--verbose=1 build/package/mtl-2.1.3.1/inplace.conf
Exit code: 1
Stderr:
mtl-2.1.3.1: package(s) with this id already exist: mtl-2.1.3.1

Any suggestions on how to get past this?

Thanks,
Michael Martin

Nicolaas du Preez | 11 Oct 15:02 2014
Picon

"Delegating" class instance definition

data Cycle = Cycle { name :: String, size :: Double } deriving (Eq, Show)

instance Ord Cycle where
	(<) a b = (<) (size a) (size b) -- problem statement!
	…

How can I specify that, on the right-hand side of the problem statement, I refer to (<) :: Double -> Double ->
Bool instead of (<) :: Cycle -> Cycle -> Bool?

Is there another way to state that "Cycle is an instance of Ord based on size"?

Gmane