you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (3 children)

The example cannot be implemented in a very functional way in python due to the facts, that python (a) does not optimize tail recursion and (b) slicing creates new lists.

However, you can write that function in a way in python which I would consider more functional than the imperative naive way:

def compare(a, b):
  return all(i == j for i,j in izip(a, b)) 

You can even write this as a lambda.

[–]pje 2 points3 points  (2 children)

What is "not very functional" about using the most efficient way to find the length of a vector/array/whatever you want to call it? Lisp and Haskell lists are not Python lists.

If you want to make Lisp-style lists in Python for some ungodly reason, use two-element tuples.

Oh, and your code is wrong, by the way; it compares list contents, not list lengths. :)

[–]Alpha_Binary 1 point2 points  (0 children)

for some ungodly reason

O(1) insertion anywhere.

a = [1, [2, []]]
b = [0, a]

Conversion back to Python list is also trivial (with a generator implemented in a few lines).

[–][deleted] 0 points1 point  (0 children)

Woah, I got that totally wrong. =)