Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1. Re:  dealing with chained functions (Chadda? Fouch?)
   2. Re:  dealing with chained functions (Alec Benzer)
   3.  Pattern matching on binary trees (Rohit Garg)
   4.  Meaning of (Eq a) (Rohit Garg)
   5. Re:  Meaning of (Eq a) (Johan Jeuring)
   6. Re:  Meaning of (Eq a) (Lakshmi Narasimhan)
   7. Re:  Meaning of (Eq a) (lakshminaras2...@gmail.com)
   8. Re:  Pattern matching on binary trees (lakshminaras2...@gmail.com)


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

Message: 1
Date: Sun, 5 Sep 2010 09:41:21 +0200
From: Chadda? Fouch? <chaddai.fou...@gmail.com>
Subject: Re: [Haskell-beginners] dealing with chained functions
To: Alec Benzer <alecben...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <aanlktimv0fzxwo81+k3ftejd2=narfk2vcz4=_hhn...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sat, Sep 4, 2010 at 10:06 AM, Alec Benzer <alecben...@gmail.com> wrote:
> No, ya, I get that differences between the two methods, I was asking
> if one of them was generally considered better practice than the
> other.

I think your question is mixing two different concern : A) how you
should write your Haskell functions so that you reap most benefit from
its functional nature, and B) what functions you should expose in the
interface of your libraries.

The answer to A) is that you should use the solution that is more
composable (the former), the answer to B) really depends on what your
library does and the philosophy of your interface... In other words,
it may be better in your specific case to expose the latter functions
(probably not often but it may happen) but the internals of your
library should most likely be written in the former style (it is easy
to form the latter functions from the former, while the opposite is
quite impossible).

-- 
Jedaï


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

Message: 2
Date: Sun, 5 Sep 2010 03:50:25 -0400
From: Alec Benzer <alecben...@gmail.com>
Subject: Re: [Haskell-beginners] dealing with chained functions
To: Chadda? Fouch? <chaddai.fou...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <aanlktikvfyq6tzvo3nowfgjc2lr2efa_znvubcz99...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

A) is all I was really asking. I'm not writing a library or exposing
any functions. I was concerned with how I should write the internals,
although I see how in certain contexts the latter form may be
preferable if I was writing a library.

On Sun, Sep 5, 2010 at 3:41 AM, Chaddaï Fouché <chaddai.fou...@gmail.com> wrote:
> On Sat, Sep 4, 2010 at 10:06 AM, Alec Benzer <alecben...@gmail.com> wrote:
>> No, ya, I get that differences between the two methods, I was asking
>> if one of them was generally considered better practice than the
>> other.
>
> I think your question is mixing two different concern : A) how you
> should write your Haskell functions so that you reap most benefit from
> its functional nature, and B) what functions you should expose in the
> interface of your libraries.
>
> The answer to A) is that you should use the solution that is more
> composable (the former), the answer to B) really depends on what your
> library does and the philosophy of your interface... In other words,
> it may be better in your specific case to expose the latter functions
> (probably not often but it may happen) but the internals of your
> library should most likely be written in the former style (it is easy
> to form the latter functions from the former, while the opposite is
> quite impossible).
>
> --
> Jedaï
>


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

Message: 3
Date: Sun, 5 Sep 2010 14:18:50 +0530
From: Rohit Garg <rpg....@gmail.com>
Subject: [Haskell-beginners] Pattern matching on binary trees
To: beginners@haskell.org
Message-ID:
        <aanlktikonlw_dwqxow-nd4aih_g2s_p0rscn2wmne...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

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/


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

Message: 4
Date: Sun, 5 Sep 2010 14:28:09 +0530
From: Rohit Garg <rpg....@gmail.com>
Subject: [Haskell-beginners] Meaning of (Eq a)
To: beginners@haskell.org
Message-ID:
        <aanlktin=dcg44m1yqgdbusyk3zmhdrn4smw9yhhf6...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

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 equlity 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


Thanks,
-- 
Rohit Garg

http://rpg-314.blogspot.com/


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

Message: 5
Date: Sun, 5 Sep 2010 11:18:42 +0200
From: Johan Jeuring <joh...@cs.uu.nl>
Subject: Re: [Haskell-beginners] Meaning of (Eq a)
To: Rohit Garg <rpg....@gmail.com>
Cc: beginners@haskell.org
Message-ID: <34f37cd3-a74a-4238-8faa-d5b9c54f9...@cs.uu.nl>
Content-Type: text/plain; charset=US-ASCII; format=flowed

> 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 equlity 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

Maybe GHC infers this for you?

Inspect the type of sumList by means of

 >:t sumList

Cheers,

Johan


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

Message: 6
Date: Sun, 05 Sep 2010 14:49:50 +0530
From: Lakshmi Narasimhan <lakshminaras2...@gmail.com>
Subject: Re: [Haskell-beginners] Meaning of (Eq a)
To: Rohit Garg <rpg....@gmail.com>
Cc: beginners@haskell.org
Message-ID: <4c8360b6.2050...@gmail.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

  On 09/05/2010 02:28 PM, Rohit Garg wrote:
> Hi,
>
> 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 equlity 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
>
>
> Thanks,
Hi Rohit,
You are correct about the assumption. Giving (Eq a) is constraining the 
set of types that can be used with to the palindrome function. The 
elements of the type a can be tested for equality.

The compiler can perform type inference. It will find out the type 
signature if you do not provide one. Assume that you did not provide the 
type signature for  the isPalindrome function, here is what I get when I 
loaded this code into ghci.
     *Main> :t isPalindrome
     isPalindrome :: (Eq t) => [t] -> Bool

For the sumList function, since you did not provide the type 
information, the compiler will infer the type for the argument and 
result of the function. However, as you point out correctly, not all 
types can be added. Hence  constraint (Num t) will be added to the type 
signature. This means that the elements  of the type "t"  can be added. 
If not, then that type cannot be used and  a compiler error will result.

     sumList :: (Num t) => [t] -> t

Hope that helps
-Lakshmi Narasimhan


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

Message: 7
Date: Sun, 5 Sep 2010 14:54:14 +0530
From: "lakshminaras2...@gmail.com" <lakshminaras2...@gmail.com>
Subject: Re: [Haskell-beginners] Meaning of (Eq a)
To: Rohit Garg <rpg....@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <aanlktik4ghnkt67v0d_jqf89ww+ffyctb_gtrhljt...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Actually (Num t)  would mean that certain other functions are also
applicable to the elements of the type t, not only addition.

On Sun, Sep 5, 2010 at 2:49 PM, Lakshmi Narasimhan <
lakshminaras2...@gmail.com> wrote:

> This means that the elements  of the type "t"  can be added.




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

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

Message: 8
Date: Sun, 5 Sep 2010 15:16:49 +0530
From: "lakshminaras2...@gmail.com" <lakshminaras2...@gmail.com>
Subject: Re: [Haskell-beginners] Pattern matching on binary trees
To: Rohit Garg <rpg....@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <aanlktikphnawpqfmfduty6sxqmqy6oeo8hiwc_49g...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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 <rpg....@gmail.com> 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
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



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

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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


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

Reply via email to