Skip to content

Instantly share code, notes, and snippets.

@tiensonqin
Created February 21, 2014 06:22
Show Gist options
  • Save tiensonqin/9129711 to your computer and use it in GitHub Desktop.
Save tiensonqin/9129711 to your computer and use it in GitHub Desktop.
%% 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