Re: [Haskell-cafe] Re: Ultra-newbie Question

2010-09-20 Thread Luke Palmer
On Sun, Sep 19, 2010 at 5:01 PM, Henrique Becker
henriquebecke...@gmail.com wrote:
 Why not?

 import Data.Number.Nat as N

 lastN :: Integral b = b - [a] - [a]
 lastN n xs = N.drop (N.length xs - n') xs
        where n' = N.toNat n

Wow.  That is gorgeous!  I think it's basically the same idea as my
zipWith implementation, but it is so much clearer here.  Thanks :-)

Luke
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Ultra-newbie Question

2010-09-20 Thread Luke Palmer
On Mon, Sep 20, 2010 at 5:11 PM, Luke Palmer lrpal...@gmail.com wrote:
 On Sun, Sep 19, 2010 at 5:01 PM, Henrique Becker
 henriquebecke...@gmail.com wrote:
 Why not?

 import Data.Number.Nat as N

 lastN :: Integral b = b - [a] - [a]
 lastN n xs = N.drop (N.length xs - n') xs
        where n' = N.toNat n

 Wow.  That is gorgeous!  I think it's basically the same idea as my
 zipWith implementation, but it is so much clearer here.  Thanks :-)

Er forget that zipWith comment.  It is quite unlike that. But it is
still beautiful and efficient.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Ultra-newbie Question

2010-09-20 Thread Henrique Becker
Thanks, It's my first post.

If you not import Prelude is more clear (N. is horrible):

import Prelude ((-), Integral)
import Data.Number.Nat (drop, length, toNat)

lastN :: Integral b = b - [a] - [a]
lastN n xs = drop (length xs - n') xs
where n' = toNat n

P.S.: You benchmarked? I didn't...

2010/9/20, Luke Palmer lrpal...@gmail.com:
 On Mon, Sep 20, 2010 at 5:11 PM, Luke Palmer lrpal...@gmail.com wrote:
 On Sun, Sep 19, 2010 at 5:01 PM, Henrique Becker
 henriquebecke...@gmail.com wrote:
 Why not?

 import Data.Number.Nat as N

 lastN :: Integral b = b - [a] - [a]
 lastN n xs = N.drop (N.length xs - n') xs
        where n' = N.toNat n

 Wow.  That is gorgeous!  I think it's basically the same idea as my
 zipWith implementation, but it is so much clearer here.  Thanks :-)

 Er forget that zipWith comment.  It is quite unlike that. But it is
 still beautiful and efficient.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Ultra-newbie Question

2010-09-19 Thread Henrique Becker
Why not?

import Data.Number.Nat as N

lastN :: Integral b = b - [a] - [a]
lastN n xs = N.drop (N.length xs - n') xs
where n' = N.toNat n

Not import Prelude maybe make more clear.

[]'s
Henrique Becker
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Ultra-newbie Question

2010-09-18 Thread Maciej Piechotka
On Sat, 2010-09-18 at 03:51 -0400, Christopher Tauss wrote:
 Hello Haskell Community -
  
 I am a professional programmer with 11 years experience, yet I just do
 not seem to be able to get the hang of even simple things in Haskell.
 I am trying to write a function that takes a list and returns the last
 n elements.
  
 There may be a function which I can just call that does that, but I am
 trying to roll my own just to understand the concept.
  
 Let's call the function n_lastn and, given a list  [1,2,3,4,5], I
 would like 
 n_lastn 3 = [3,4,5]
  
 Seems like it would be something like:
  
 n_lastn:: [a]-Int-[a]
 n_lastn 1 (xs) = last(xs)
 n_lastn n (x:xs) = 
  
 The issue is I do not see how you can store the last elements of the
 list.
  
 Thanks in advance.
  
 ctauss

I'll add my $0.03 - unless you are doing it to learn about lists rethink
your approach. Taking k elements from end of n-element list will be O(n)
operation.

For example with appropriate structures (like finger trees) it would
look like O(k) operation.

Regards


signature.asc
Description: This is a digitally signed message part
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Ultra-newbie Question

2010-09-18 Thread Gregory Crosswhite

 Translation:  Look at Data.Sequence sometime.

On 9/18/10 11:15 AM, Maciej Piechotka wrote:

On Sat, 2010-09-18 at 03:51 -0400, Christopher Tauss wrote:

Hello Haskell Community -

I am a professional programmer with 11 years experience, yet I just do
not seem to be able to get the hang of even simple things in Haskell.
I am trying to write a function that takes a list and returns the last
n elements.

There may be a function which I can just call that does that, but I am
trying to roll my own just to understand the concept.

Let's call the function n_lastn and, given a list  [1,2,3,4,5], I
would like
n_lastn 3 = [3,4,5]

Seems like it would be something like:

n_lastn:: [a]-Int-[a]
n_lastn 1 (xs) = last(xs)
n_lastn n (x:xs) = 

The issue is I do not see how you can store the last elements of the
list.

Thanks in advance.

ctauss

I'll add my $0.03 - unless you are doing it to learn about lists rethink
your approach. Taking k elements from end of n-element list will be O(n)
operation.

For example with appropriate structures (like finger trees) it would
look like O(k) operation.

Regards


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Ultra-newbie Question

2010-09-18 Thread Gregory Crosswhite

 Translation:  Look at Data.Sequence sometime.

On 9/18/10 11:15 AM, Maciej Piechotka wrote:

On Sat, 2010-09-18 at 03:51 -0400, Christopher Tauss wrote:

Hello Haskell Community -

I am a professional programmer with 11 years experience, yet I just do
not seem to be able to get the hang of even simple things in Haskell.
I am trying to write a function that takes a list and returns the last
n elements.

There may be a function which I can just call that does that, but I am
trying to roll my own just to understand the concept.

Let's call the function n_lastn and, given a list  [1,2,3,4,5], I
would like
n_lastn 3 = [3,4,5]

Seems like it would be something like:

n_lastn:: [a]-Int-[a]
n_lastn 1 (xs) = last(xs)
n_lastn n (x:xs) = 

The issue is I do not see how you can store the last elements of the
list.

Thanks in advance.

ctauss

I'll add my $0.03 - unless you are doing it to learn about lists rethink
your approach. Taking k elements from end of n-element list will be O(n)
operation.

For example with appropriate structures (like finger trees) it would
look like O(k) operation.

Regards


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe