a "take" function edge case

35 views
Skip to first unread message

Andrei Volkov

unread,
Oct 10, 2012, 4:43:01 PM10/10/12
to ela...@googlegroups.com
Hello again. The more I compare Ela to Haskell the more I like Ela's dynamic typing. Why, in Haskell you can't even have a list of elements of different types! Who needs such lists?! ))

Speaking about lists, I noticed a funny difference between Ela's and Haskell's "take" function.

In Haskell, this returns [1,2,3,4]:
take 5 [1,2,3,4]

In Ela, this returns []:
open list
take
5 [1,3,4,5]

is this by design or by accident?

~Andrei

Vasily Voronkov

unread,
Oct 10, 2012, 4:59:31 PM10/10/12
to ela...@googlegroups.com

No, this is a bug in take function in list module :)

You can add it to tracker.

 

A correct version is:

 

take = take []

  where

    take ys 0 _       = reverse ys

    take ys n (x::xs) = take (x::ys) (n - 1) xs

    take ys _ []      = reverse ys

 

Or naïve version:

 

take _ [] = []

take 0 _ = []

take n (x::xs) = x :: take (n - 1) xs

--
 
 
 

Reply all
Reply to author
Forward
0 new messages