Created
February 21, 2014 06:22
-
-
Save tiensonqin/9129711 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% clojure version, recursion is all you need | |
(defn take | |
"Returns a lazy sequence of the first n items in coll, or all items if | |
there are fewer than n." | |
{:added "1.0" | |
:static true} | |
[n coll] | |
(lazy-seq | |
(when (pos? n) | |
(when-let [s (seq coll)] | |
(cons (first s) (take (dec n) (rest s))))))) | |
%% erlang version, pattern matching, just not lazy | |
sublist_2([H|T], L) when L > 0 -> | |
[H|sublist_2(T, L-1)]; | |
sublist_2(_, 0) -> | |
[]; | |
sublist_2(List, L) when is_list(List), L > 0 -> | |
[]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment