Re: [Haskell-cafe] Ultra-newbie Question

2010-09-21 Thread Christopher Tauss
Thanks Richard and the others who responded to my query. I truly appreciate you taking the time and effort to respond to me (and the community) with your thoughts. I had been reading about recursion, and was thinking only of that approach to solve this. My main reson for looking into Haskell is

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-20 Thread Henning Thielemann
Christopher Tauss schrieb: 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. Looking through the glasses of lazy

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-20 Thread Alberto G. Corona
2010/9/18 Daniel Fischer daniel.is.fisc...@web.de n_lastn n = reverse . take n . reverse Which is the most elegant definition, but it's an O(length list) space operation (as are all others proposed so far). T No!. You forget laziness!. it is 0(n) with n= the parameter passed to

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-20 Thread Henning Thielemann
Luke Palmer schrieb: I think this is O(n) time, O(1) space (!). lastk :: Int - [a] - [a] lastk k xs = last $ zipWith const (properTails xs) (drop k xs) where properTails = tail . tails If (drop k xs) is empty, this yields an error when calling 'last'. This might be a bug or a feature.

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-20 Thread Jean-Marie Gaillourdet
Hi Alberto, On 20.09.2010, at 10:53, Alberto G. Corona wrote: 2010/9/18 Daniel Fischer daniel.is.fisc...@web.de n_lastn n = reverse . take n . reverse Which is the most elegant definition, but it's an O(length list) space operation (as are all others proposed so far). T No!. You

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-20 Thread James Andrew Cook
On Sep 20, 2010, at 5:10 AM, Jean-Marie Gaillourdet wrote: Hi Alberto, On 20.09.2010, at 10:53, Alberto G. Corona wrote: 2010/9/18 Daniel Fischer daniel.is.fisc...@web.de n_lastn n = reverse . take n . reverse Which is the most elegant definition, but it's an O(length list) space

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-20 Thread Jean-Marie Gaillourdet
Hi James, On 20.09.2010, at 15:20, James Andrew Cook wrote: Lazyness helps helps to reduce work if your input list is lazily constructed and your function forces the returned element. Then you don't have to force all elements of the list, only the last one. Let's say l = [e_0, ..., e_n].

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-20 Thread Daniel Fischer
On Monday 20 September 2010 15:20:53, James Andrew Cook wrote: On Sep 20, 2010, at 5:10 AM, Jean-Marie Gaillourdet wrote: Hi Alberto, On 20.09.2010, at 10:53, Alberto G. Corona wrote: 2010/9/18 Daniel Fischer daniel.is.fisc...@web.de n_lastn n = reverse . take n . reverse

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-20 Thread Daniel Peebles
Have we put off the ultra-newbie by derailing his simple question into a discussion on subtle issues he shouldn't care about this early on? On Mon, Sep 20, 2010 at 3:49 PM, Daniel Fischer daniel.is.fisc...@web.dewrote: On Monday 20 September 2010 15:20:53, James Andrew Cook wrote: On Sep 20,

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-20 Thread James Andrew Cook
On Sep 20, 2010, at 9:49 AM, Daniel Fischer wrote: which I am inclined to believe. Your 'f' should also run in O(1) space. Alas, no. At least with GHC (and I don't see how it could be otherwise), reverse is always an O(length xs) space operation. reverse :: [a] - [a]

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-20 Thread Richard O'Keefe
On Sep 18, 2010, at 7:51 PM, 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

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-19 Thread Aai
I like this one! Here's a variant using fold: lastk :: Int - [a] - [a] lastk k xs = foldl' (const.tail) xs (drop k xs) or point free: lastk = ap (foldl' (const. tail)). drop Hallo Luke Palmer, je schreef op 18-09-10 22:42: I think this is O(n) time, O(1) space (!). lastk :: Int - [a] - [a]

[Haskell-cafe] Ultra-newbie Question

2010-09-18 Thread Christopher Tauss
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

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-18 Thread Ivan Lazar Miljenovic
On 18 September 2010 17:51, Christopher Tauss ctau...@gmail.com 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

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-18 Thread David Terei
Here is a more manual way to do it, hopefully this shows the approach required. You don't need to store anything, just keep removing the head of the list until its of the size you want. n_lastn :: Int - [a] - [a] n_lastn n xs = let len = length xs - n drp = if len 0 then 0 else len

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-18 Thread Nils Schweinsberg
Am 18.09.2010 09:51, schrieb Christopher Tauss: I am trying to write a function that takes a list and returns the last n elements. last_n n = fst . foldr step ([], n) where step _ (xs, 0) = (xs, 0) step x (xs, n) = (x:xs, n-1) ___

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-18 Thread Jake McArthur
On 09/18/2010 02:51 AM, Christopher Tauss wrote: I am trying to write a function that takes a list and returns the last n elements. This may just be for the sake of learning, in which case this is fine, but usually, needing to do this would be a sign that you are using lists improperly

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-18 Thread Christopher Tauss
Thanks you Ivan and David for clarifying this. Best Regards, Chris On Sat, Sep 18, 2010 at 3:55 AM, Ivan Lazar Miljenovic ivan.miljeno...@gmail.com wrote: On 18 September 2010 17:51, Christopher Tauss ctau...@gmail.com wrote: Hello Haskell Community - I am a professional programmer

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-18 Thread Daniel Fischer
On Saturday 18 September 2010 19:44:38, Jake McArthur wrote: On 09/18/2010 02:51 AM, Christopher Tauss wrote: I am trying to write a function that takes a list and returns the last n elements. This may just be for the sake of learning, in which case this is fine, but usually, needing to do

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-18 Thread Luke Palmer
I think this is O(n) time, O(1) space (!). lastk :: Int - [a] - [a] lastk k xs = last $ zipWith const (properTails xs) (drop k xs) where properTails = tail . tails Luke On Sat, Sep 18, 2010 at 1:51 PM, Daniel Fischer daniel.is.fisc...@web.de wrote: On Saturday 18 September 2010 19:44:38,

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-18 Thread Daniel Fischer
On Saturday 18 September 2010 22:42:57, Luke Palmer wrote: I think this is O(n) time, O(1) space (!). lastk :: Int - [a] - [a] lastk k xs = last $ zipWith const (properTails xs) (drop k xs)     where properTails = tail . tails Luke No, it's O(k) too. You zip [[x_n, x_{n+1}, ... ], ... ]

Re: [Haskell-cafe] Ultra-newbie Question

2010-09-18 Thread Alexander Solla
On Sep 18, 2010, at 12:51 AM, Christopher Tauss wrote: 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.