Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Pattern matching on binary trees ([email protected])
   2. Re:  Meaning of (Eq a) (Hein Hundal)
   3. Re:  Meaning of (Eq a) (Markus L?ll)
   4.  Trouble in installing Packages (Lorenzo Isella)
   5. Re:  Meaning of (Eq a) (Brandon S Allbery KF8NH)


----------------------------------------------------------------------

Message: 1
Date: Sun, 5 Sep 2010 15:37:03 +0530
From: "[email protected]" <[email protected]>
Subject: Re: [Haskell-beginners] Pattern matching on binary trees
To: Rohit Garg <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Hi Rohit,
You can probably ignore the first line of my reply, if it feels a bit
confusing :)

On Sun, Sep 5, 2010 at 3:16 PM, [email protected] <
[email protected]> wrote:

> The idea behind pattern matching is that you can use any of the data
> constructor for the type to dismantle the value coming in.
>
> For depthTree, the type of value expected is Tree a. The Node constructor
> is used to dismantle the incoming value into three parts and bind it to
> variables x u v. Note here that depthTree accepts only one argument as input
> and the type of that value is (Tree a)
>
> When you do not want to dismantle the incoming value, then you would not
> use any data constructor. Your function would then become,
>     depthTree :: Tree a -> Int
>     depthTree x = ..
>
> You cannot use depthTree (x u v) because the compiler would then try to
> look for a data constructor x . And in Haskell, data constructor names begin
> with a uppercase letter.
>
> You can refer to this page on Haskell wiki 
> bookhttp://en.wikibooks.org/wiki/Haskell/Pattern_matching for
> more information.
>
>
> On Sun, Sep 5, 2010 at 2:18 PM, Rohit Garg <[email protected]> wrote:
>
>> Hi,
>>
>> I am working on exercises in RWH, chapter 3 and I have a a question
>> about the pattern matching in the depth-of-tree question.
>>
>> The tree datatype is defined as
>>
>> data Tree a = Node a (Tree a) (Tree a)
>>            | Empty
>>              deriving (Show)
>>
>> And to pattern match on it, I need to write
>>
>> depthTree (Node x u v) = 1 + max (depthTree u) (depthTree v)
>>
>> My question is why do I need to prefix x with Node? Why can't it
>> pattern match on depthTree (x u v)? More generally, where do I need to
>> put data constructors in pattern matches?
>>
>> --
>> Rohit Garg
>>
>> http://rpg-314.blogspot.com/
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
>
> --
> Regards
> Lakshmi Narasimhan T V
>



-- 
Regards
Lakshmi Narasimhan T V
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100905/fb6ce170/attachment-0001.html

------------------------------

Message: 2
Date: Sun, 5 Sep 2010 03:20:46 -0700 (PDT)
From: Hein Hundal <[email protected]>
Subject: Re: [Haskell-beginners] Meaning of (Eq a)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

> Date: Sun, 5 Sep 2010 14:28:09 +0530
> From: Rohit Garg <[email protected]>
>
> RWH: chapter 3 - in question 5, you have to write a
> function which determines if a list is a palindrome. 
> Here is my solution
> 
> isPalindrome :: (Eq a) => [a] -> Bool
> isPalindrome [] = False
> isPalindrome x = compareLists x (reverse x)
>    where compareLists [x] [y] = x == y
>          compareLists (x:xs) (y:ys) = if x == y      
>        then compareLists xs ys
>        else False
> 
> Although it works, my question is why ghci refuses to 
> run it without the "(Eq a) => " being added to the type 
> signature of the function. Presumably, it is to let ghc 
> know that you can perform equality tests on a. If so, 
> then why does the sumList function below work without 
> any type signature of any kind? I haven't told ghc 
> that the input list elements can be added together.
> 
> sumList []     = 0
> sumList (x:xs) = x + sumList xs

When I compile isPalindrome with ghc 6.12.1, I do not get any errors even if I 
drop the type signature.  If I drop the type signature and use ghci to get the 
type signature (":t isPalindrome"), then I get 


*Main> :t isPalindrome
isPalindrome :: (Eq t) => [t] -> Bool


So, it seems that ghc 6.12.1 correctly infers the types.

Cheers,
Hein

PS:  Haskell does equality of lists, so you can also use the definition 

isPalindrome v = v == reverse v




      


------------------------------

Message: 3
Date: Sun, 5 Sep 2010 14:12:24 +0300
From: Markus L?ll <[email protected]>
Subject: Re: [Haskell-beginners] Meaning of (Eq a)
To: Hein Hundal <[email protected]>, [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

I have not tested, but I suspect, that if you don't provide a type
signature for either isPalindrome or sumList, it will get inferred
correctly. The problem is that if you *do* provide a typesignature for
either of them, you *have* to give it a properly constrained
typesignatures.

I think for sumList a signature of [a] -> a does not work, because you
use the adding function, which has type (Num a) -> a -> a -> a. So
when providing a signature for sumList you have to add the Num a
constraint too. The same goes for isPlindrome, where Eq a is
neccessary because of the type of ==.

Like, for isPalindrome the signature [a] -> Bool is not correct,
because it says any 'a' will work (because it is not constrained), but
any 'a' wont work -- it has to be an equitable 'a', thus the (Eq a)
constraint.

The point is, that when providing a signature, it has to be
sufficently (or more) constrained -- GHC won't patch it wor you, but
if the signature is missing it will (try to) infer it.


Markus



On Sun, Sep 5, 2010 at 1:20 PM, Hein Hundal <[email protected]> wrote:
>> Date: Sun, 5 Sep 2010 14:28:09 +0530
>> From: Rohit Garg <[email protected]>
>>
>> RWH: chapter 3 - in question 5, you have to write a
>> function which determines if a list is a palindrome.
>> Here is my solution
>>
>> isPalindrome :: (Eq a) => [a] -> Bool
>> isPalindrome [] = False
>> isPalindrome x = compareLists x (reverse x)
>>    where compareLists [x] [y] = x == y
>>          compareLists (x:xs) (y:ys) = if x == y
>>        then compareLists xs ys
>>        else False
>>
>> Although it works, my question is why ghci refuses to
>> run it without the "(Eq a) => " being added to the type
>> signature of the function. Presumably, it is to let ghc
>> know that you can perform equality tests on a. If so,
>> then why does the sumList function below work without
>> any type signature of any kind? I haven't told ghc
>> that the input list elements can be added together.
>>
>> sumList []     = 0
>> sumList (x:xs) = x + sumList xs
>
> When I compile isPalindrome with ghc 6.12.1, I do not get any errors even if 
> I drop the type signature.  If I drop the type signature and use ghci to get 
> the type signature (":t isPalindrome"), then I get
>
>
> *Main> :t isPalindrome
> isPalindrome :: (Eq t) => [t] -> Bool
>
>
> So, it seems that ghc 6.12.1 correctly infers the types.
>
> Cheers,
> Hein
>
> PS:  Haskell does equality of lists, so you can also use the definition
>
> isPalindrome v = v == reverse v
>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


------------------------------

Message: 4
Date: Sun, 05 Sep 2010 16:05:27 +0200
From: Lorenzo Isella <[email protected]>
Subject: [Haskell-beginners] Trouble in installing Packages
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Dear All,
I am quite new to Haskell and have only very recently found out about cabal.
I am interested in some haskell packages for scientific applications, 
but I am experiencing some problems in installing them on my machine 
(ubuntu 10.04, amd64 architecture).
For instance, I pasted below the output of my attempts to install 
hmatrix-static.
Any idea about how to fix this?
Cheers

Lorenzo



$ cabal install hmatrix-static
Resolving dependencies...
Configuring array-0.2.0.0...
Preprocessing library array-0.2.0.0...
Building array-0.2.0.0...
[ 1 of 10] Compiling Data.Array       ( Data/Array.hs, 
dist/build/Data/Array.o )

Data/Array.hs:1:0:
     Warning: Module `Prelude' is deprecated:
                You are using the old package `base' version 3.x.
                Future GHC versions will not support base version 3.x. You
                should update your code to use the new base version 4.x.

Data/Array/IO.hs:1:15:
     Warning: -#include is deprecated: No longer has any effect

Data/Array/IO/Internals.hs:1:15:
     Warning: -#include is deprecated: No longer has any effect
[ 2 of 10] Compiling Data.Array.Base  ( Data/Array/Base.hs, 
dist/build/Data/Array/Base.o )

Data/Array/Base.hs:1:0:
     Warning: Module `Prelude' is deprecated:
                You are using the old package `base' version 3.x.
                Future GHC versions will not support base version 3.x. You
                should update your code to use the new base version 4.x.

Data/Array/Base.hs:1177:8:
     Warning: Bindings containing unlifted types must use an outermost 
bang pattern:
                  { W# e# = if initialValue then maxBound else 0 }
              *** This will be an error in GHC 6.14! Fix your code now!
     In the definition of `newArray':
         newArray (l, u) initialValue
                    = ST $ \ s1# -> case safeRangeSize (l, u) of { 
(n@(I# n#)) -> ... }
                    where
                        W# e# = if initialValue then maxBound else 0
     In the instance declaration for `MArray (STUArray s) Bool (ST s)'

Data/Array/Base.hs:1489:8:
     Warning: Bindings containing unlifted types must use an outermost 
bang pattern:
                  { I# last# = 8 * 8 - 1 }
              *** This will be an error in GHC 6.14! Fix your code now!
     In the definition of `bOOL_SCALE':
         bOOL_SCALE n#
                      = (n# +# last#) `uncheckedIShiftRA#` 3#
                      where
                          I# last# = 8 * 8 - 1

Data/Array/Base.hs:1491:8:
     Warning: Bindings containing unlifted types must use an outermost 
bang pattern:
                  { I# last# = 8 * 8 - 1 }
              *** This will be an error in GHC 6.14! Fix your code now!
     In the definition of `bOOL_WORD_SCALE':
         bOOL_WORD_SCALE n#
                           = bOOL_INDEX (n# +# last#)
                           where
                               I# last# = 8 * 8 - 1

Data/Array/Base.hs:1492:37:
     Warning: Bindings containing unlifted types must use an outermost 
bang pattern:
                  { I# scale# = 8 }
              *** This will be an error in GHC 6.14! Fix your code now!
     In the definition of `wORD_SCALE':
         wORD_SCALE n#
                      = scale# *# n#
                      where
                          I# scale# = 8

Data/Array/Base.hs:1493:37:
     Warning: Bindings containing unlifted types must use an outermost 
bang pattern:
                  { I# scale# = 8 }
              *** This will be an error in GHC 6.14! Fix your code now!
     In the definition of `dOUBLE_SCALE':
         dOUBLE_SCALE n#
                        = scale# *# n#
                        where
                            I# scale# = 8

Data/Array/Base.hs:1494:37:
     Warning: Bindings containing unlifted types must use an outermost 
bang pattern:
                  { I# scale# = 4 }
              *** This will be an error in GHC 6.14! Fix your code now!
     In the definition of `fLOAT_SCALE':
         fLOAT_SCALE n#
                       = scale# *# n#
                       where
                           I# scale# = 4

Data/Array/Base.hs:1505:8:
     Warning: Bindings containing unlifted types must use an outermost 
bang pattern:
                  { W# mask# = 8 * 8 - 1 }
              *** This will be an error in GHC 6.14! Fix your code now!
     In the definition of `bOOL_BIT':
         bOOL_BIT n#
                    = int2Word# 1#
                    `uncheckedShiftL#`
                      (word2Int# (int2Word# n# `and#` mask#))
                    where
                        W# mask# = 8 * 8 - 1

Data/Array/Base.hs:1506:47:
     Warning: Bindings containing unlifted types must use an outermost 
bang pattern:
                  { W# mb# = maxBound }
              *** This will be an error in GHC 6.14! Fix your code now!
     In the definition of `bOOL_NOT_BIT':
         bOOL_NOT_BIT n#
                        = bOOL_BIT n# `xor#` mb#
                        where
                            W# mb# = maxBound
[ 3 of 10] Compiling Data.Array.IArray ( Data/Array/IArray.hs, 
dist/build/Data/Array/IArray.o )

Data/Array/IArray.hs:1:0:
     Warning: Module `Prelude' is deprecated:
                You are using the old package `base' version 3.x.
                Future GHC versions will not support base version 3.x. You
                should update your code to use the new base version 4.x.
[ 4 of 10] Compiling Data.Array.MArray ( Data/Array/MArray.hs, 
dist/build/Data/Array/MArray.o )

Data/Array/MArray.hs:1:0:
     Warning: Module `Prelude' is deprecated:
                You are using the old package `base' version 3.x.
                Future GHC versions will not support base version 3.x. You
                should update your code to use the new base version 4.x.
[ 5 of 10] Compiling Data.Array.IO.Internals ( 
Data/Array/IO/Internals.hs, dist/build/Data/Array/IO/Internals.o )

Data/Array/IO/Internals.hs:1:0:
     Warning: Module `Prelude' is deprecated:
                You are using the old package `base' version 3.x.
                Future GHC versions will not support base version 3.x. You
                should update your code to use the new base version 4.x.
[ 6 of 10] Compiling Data.Array.IO    ( Data/Array/IO.hs, 
dist/build/Data/Array/IO.o )

Data/Array/IO.hs:73:13:
     `haFD' is not a (visible) field of constructor `Handle__'

Data/Array/IO.hs:73:22:
     `haBuffer' is not a (visible) field of constructor `Handle__'

Data/Array/IO.hs:73:36:
     `haIsStream' is not a (visible) field of constructor `Handle__'

Data/Array/IO.hs:74:5: Not in scope: data constructor `Buffer'

Data/Array/IO.hs:74:13:
     `bufBuf' is not a (visible) field of constructor `Buffer'

Data/Array/IO.hs:74:25:
     `bufWPtr' is not a (visible) field of constructor `Buffer'

Data/Array/IO.hs:74:36:
     `bufRPtr' is not a (visible) field of constructor `Buffer'

Data/Array/IO.hs:75:4: Not in scope: `bufferEmpty'

Data/Array/IO.hs:82:24:
     `bufWPtr' is not a (visible) constructor field name

Data/Array/IO.hs:82:35:
     `bufRPtr' is not a (visible) constructor field name

Data/Array/IO.hs:86:24:
     `bufRPtr' is not a (visible) constructor field name

Data/Array/IO.hs:95:27:
     Not in scope: type constructor or class `RawBuffer'

Data/Array/IO.hs:101:10: Not in scope: `readRawBuffer'

Data/Array/IO.hs:125:22:
     `haFD' is not a (visible) field of constructor `Handle__'

Data/Array/IO.hs:125:31:
     `haBuffer' is not a (visible) field of constructor `Handle__'

Data/Array/IO.hs:125:45:
     `haIsStream' is not a (visible) field of constructor `Handle__'

Data/Array/IO.hs:127:18: Not in scope: data constructor `Buffer'

Data/Array/IO.hs:127:26:
     `bufBuf' is not a (visible) field of constructor `Buffer'

Data/Array/IO.hs:127:42:
     `bufWPtr' is not a (visible) field of constructor `Buffer'

Data/Array/IO.hs:127:53:
     `bufSize' is not a (visible) field of constructor `Buffer'

Data/Array/IO.hs:135:30:
     `bufWPtr' is not a (visible) constructor field name

Data/Array/IO.hs:142:7: Not in scope: data constructor `Buffer'

Data/Array/IO.hs:142:15:
     `bufBuf' is not a (visible) field of constructor `Buffer'

Data/Array/IO.hs:142:27:
     `bufState' is not a (visible) field of constructor `Buffer'

Data/Array/IO.hs:142:36:
     Not in scope: data constructor `WriteBuffer'

Data/Array/IO.hs:143:8:
     `bufRPtr' is not a (visible) field of constructor `Buffer'

Data/Array/IO.hs:143:19:
     `bufWPtr' is not a (visible) field of constructor `Buffer'

Data/Array/IO.hs:143:34:
     `bufSize' is not a (visible) field of constructor `Buffer'

Data/Array/IO.hs:151:22:
     Not in scope: type constructor or class `RawBuffer'

Data/Array/IO.hs:151:43:
     Not in scope: type constructor or class `RawBuffer'

Data/Array/IO.hs:153:22:
     Not in scope: type constructor or class `RawBuffer'

Data/Array/IO.hs:153:35:
     Not in scope: type constructor or class `RawBuffer'
cabal: Error: some packages failed to install:
array-0.2.0.0 failed during the building phase. The exception was:
ExitFailure 1
containers-0.3.0.0 depends on array-0.2.0.0 which failed to install.
cpphs-1.11 depends on array-0.2.0.0 which failed to install.
haskell-src-exts-1.2.0 depends on array-0.2.0.0 which failed to install.
haskell-src-meta-0.0.6 depends on array-0.2.0.0 which failed to install.
haskell98-1.0.1.1 depends on array-0.2.0.0 which failed to install.
hmatrix-0.7.2.1 depends on array-0.2.0.0 which failed to install.
hmatrix-static-0.3 depends on array-0.2.0.0 which failed to install.
packedstring-0.1.0.1 depends on array-0.2.0.0 which failed to install.
template-haskell-2.4.0.0 depends on array-0.2.0.0 which failed to install.
tfp-0.2 depends on array-0.2.0.0 which failed to install.



------------------------------

Message: 5
Date: Sun, 05 Sep 2010 10:47:40 -0400
From: Brandon S Allbery KF8NH <[email protected]>
Subject: Re: [Haskell-beginners] Meaning of (Eq a)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 9/5/10 04:58 , Rohit Garg wrote:
> Although it works, my question is why ghci refuses to run it without
> the "(Eq a) => " being added to the type signature of the function.

Haskell won't do type inference if you provide a function with a type
signature, so if you say anything at all you need to say everything about
it.  If you say *nothing* about it, as with the second function, Haskell
will infer everything including required constraints.

- -- 
brandon s. allbery     [linux,solaris,freebsd,perl]      [email protected]
system administrator  [openafs,heimdal,too many hats]  [email protected]
electrical and computer engineering, carnegie mellon university      KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkyDrYwACgkQIn7hlCsL25U60ACguFkSUsfyPdk77Q7AmA5McVcu
0S0An3lbVUtVyzGCd6SSer2bksufokpd
=NAqj
-----END PGP SIGNATURE-----


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 27, Issue 13
*****************************************

Reply via email to