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.  Use of (n + 1) as parameter - is it more efficient?
      (Angus Comber)
   2. Re:  Use of (n + 1) as parameter - is it more     efficient?
      (David McBride)
   3. Re:  Use of (n + 1) as parameter - is it more efficient?
      (Vikraman)


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

Message: 1
Date: Fri, 20 Dec 2013 18:00:08 +0000
From: Angus Comber <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] Use of (n + 1) as parameter - is it more
        efficient?
Message-ID:
        <CAAtGUhX80Ro0Dvc8PrVWhLDcKjCWqyZPNAAW1vor6kf=y=j...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

I created my own drop' function like this:

drop' :: Int -> [a] -> [a]
drop' 0 xs     = xs
drop' n []     = []
drop' n (x:xs) = drop' (n - 1) xs


But in the Haskell book I am reading it is written like this:

drop' :: Int -> [a] -> [a]
drop' 0 xs             = xs
drop' (n + 1) []       = []
drop' (n + 1) (x:xs) = drop' n xs

My version seems to work but I am concerned about my third line (drop' n []
    = []).  Is that a problem?

Why does this author use n + 1 as a parameter as opposed to n - 1 like I do
in body?

Or does it make no difference?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131220/42e8d804/attachment-0001.html>

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

Message: 2
Date: Fri, 20 Dec 2013 13:20:12 -0500
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Use of (n + 1) as parameter - is it
        more    efficient?
Message-ID:
        <can+tr43frn8uybss+sbzsal-vn8okx-doo8zi1ygt59mfeo...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

n+k patterns is a concept that used to be common in haskell but was later
determined to be a misfeature and now you have to explicitly enable it in
ghc.  It was kind of a fun thing that turned out not to be necessary.

The way that you do it is the way that it is recommended now and the code
generated should be identical.



On Fri, Dec 20, 2013 at 1:00 PM, Angus Comber <[email protected]> wrote:

> I created my own drop' function like this:
>
> drop' :: Int -> [a] -> [a]
> drop' 0 xs     = xs
> drop' n []     = []
> drop' n (x:xs) = drop' (n - 1) xs
>
>
> But in the Haskell book I am reading it is written like this:
>
> drop' :: Int -> [a] -> [a]
> drop' 0 xs             = xs
> drop' (n + 1) []       = []
> drop' (n + 1) (x:xs) = drop' n xs
>
> My version seems to work but I am concerned about my third line (drop' n
> []     = []).  Is that a problem?
>
> Why does this author use n + 1 as a parameter as opposed to n - 1 like I
> do in body?
>
> Or does it make no difference?
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131220/fa74e37d/attachment-0001.html>

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

Message: 3
Date: Fri, 20 Dec 2013 23:55:25 +0530
From: Vikraman <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Use of (n + 1) as parameter - is it
        more efficient?
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

On Fri, Dec 20, 2013 at 06:00:08PM +0000, Angus Comber wrote:
> I created my own drop' function like this:
> 
> drop' :: Int -> [a] -> [a]
> drop' 0 xs     = xs
> drop' n []     = []
> drop' n (x:xs) = drop' (n - 1) xs
> 
> 
> But in the Haskell book I am reading it is written like this:
> 
> drop' :: Int -> [a] -> [a]
> drop' 0 xs             = xs
> drop' (n + 1) []       = []
> drop' (n + 1) (x:xs) = drop' n xs
> 
> My version seems to work but I am concerned about my third line (drop' n []
>     = []).  Is that a problem?
> 
> Why does this author use n + 1 as a parameter as opposed to n - 1 like I do
> in body?
> 
> Or does it make no difference?

Using (n + 1) in the pattern match will cause the match to fail for
negative numbers. But this would also need NPlusKPatterns.

Note that in your implementation, negative values will result in an
empty list, which is different from the GHC.List version.

drop n xs     | n <= 0 =  xs
drop _ []              =  []
drop n (_:xs)          =  drop (n-1) xs

You can easily find this out using quickcheck.

?> quickCheck $ \n xs -> drop' n xs == drop n xs

-- 
Vikraman
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 230 bytes
Desc: PGP signature
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131220/b6fa72c5/attachment-0001.sig>

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

Subject: Digest Footer

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


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

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

Reply via email to