Hi,
Good inital effort. Only a couple of things to comment on:
- You're not closing the stream after you're done with it
This is a very common bug and for that reason clojure provides a macro
- with-open - that takes care of closing the resource for you.
- As you're using a string as the accumu
Hi everyone,
I'm new to Clojure, and after a lot of reading I wrote a couple of
functions.
They are working and doing what they are supposed to, but I was wondering
if the way I wrote the functions was optimal and if I made any conceptual
errors which advanced programmers avoid.
Basically: Are
Thanks for all the excellent, concise advice!
For code and idiomatic improvements, from these messages I have the
following
- For order 1 access use vector
- Use subvectors for efficient partitioning
- Case/compare is concise, powerful and readable
- Use seq idiom to proceed or return
2010/10/21 Brody Berg :
> (defn binary-search
> "Search sorted list for target using binary search technique"
Binary search is only useful on indexed data types like Clojure Vectors.
> ([m_list target]
> (if (empty? m_list)
> false
> (binary-search m_list 0 (- (
To expand on this:
1. It's better to use when (or when-not) if one branch of your if is
just a false value. E.g. you could replace (if (empty? x) false
(whatever)) with (when-not (empty? x)). However...
2. Don't use empty? if you can help it! The idiomatic way to test
whether a collection has any
You should close the parenthesis all in one line:
(defn binary-search
"Search sorted list for target using binary search technique"
([m_list target]
(if (empty? m_list)
false
(binary-search m_list 0 (- (count m_list) 1) target)))
([m_list m_left m_right
On Oct 21, 2010, at 3:28 AM, Brody Berg wrote:
>(if (== (nth m_list m_left) target)
Forgot to mention: you should use = instead of == unless you're sure you will
only ever get numeric arguments *and* profiling tells you that = is a
performance bottleneck.
--
You received this
On Oct 21, 2010, at 3:28 AM, Brody Berg wrote:
>(if (empty? m_list)
>false
>(binary-search m_list 0 (- (count m_list) 1) target))
Assuming that returning nil is OK in case of the target not being present, you
can replace this with (when (seq m_list) (binary-search
Hey,
Not sure if this is the right place for this - but I just wrote my
first function in Clojure and wanted to make sure I am on the right
track idiomatically and making full use of the language etc. I have
been able to build/run and unit test this so that's all fine. Take a
look at the
I expanded on this theme in a blog post:
http://programming-puzzler.blogspot.com/2010/07/translating-code-from-python-and-scheme.html
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note th
On Sat, Jul 24, 2010 at 9:07 AM, Gary Fredericks
wrote:
> (defn remove-first
> [syb lst]
> (let [[before after]
> (loop [b [] a lst]
> (if (empty? lst)
> [b a]
> (if (= syb (first a))
> [b (rest a)]
> (recur (con
Well obviously if you can get something to be tail-recursive you won't have
the stack overflows, and the thing in your code that prevents tail recursion
is having to cons the result of the recursive call. So let's try this:
(defn remove-first
[syb lst]
(let [[before after]
(loop [b [
@Randy Hudson
Really like that solution.
@Mark Engelberg
Thanks for the explanation
On Jul 25, 4:33 am, ataggart wrote:
> To add one small addendum to Mark's excellent comment, if you use lazy-
> seq then you don't need to worry about the nil from when
>
> On Jul 24, 12:01 pm, Mark Engelberg w
To add one small addendum to Mark's excellent comment, if you use lazy-
seq then you don't need to worry about the nil from when
On Jul 24, 12:01 pm, Mark Engelberg wrote:
> On Sat, Jul 24, 2010 at 11:45 AM, Mark Engelberg
>
> wrote:
> > The simplest translation is to wrap a lazy-seq around the
On Sat, Jul 24, 2010 at 11:45 AM, Mark Engelberg
wrote:
> The simplest translation is to wrap a lazy-seq around the last line to
> avoid the stack overflows.
Just to clarify, there are at least three reasonable places to place
the call to lazy-seq. You can put lazy-seq around the full body of
th
The simplest translation is to wrap a lazy-seq around the last line to
avoid the stack overflows.
On Sat, Jul 24, 2010 at 8:41 AM, nickikt wrote:
> (defn scheme-remove-first [syb lst]
> (if (empty? lst)
> '()
> (if (= (first lst) syb)
> (rest lst)
> (cons (first lst) (scheme-remo
Hi,
One way to prevent the stack overflows is to wrap it in a lazy seq.
For example:
(defn remove-first [x coll]
(lazy-seq
(when (seq coll)
(let [[y & ys] coll]
(if (= target y)
ys
(cons y (remove-first x ys)))
On Saturday 24 July 2010 11
Here's my take:
(defn remove-first [x coll]
(let [[pre post] (split-with #(not= x %) coll)]
(concat (pre (rest post
On Jul 24, 11:41 am, nickikt wrote:
> Hallo all,
>
> I'm working trough Essentials of Programming Languages. I'm trying to
> right a function like this one:
>
> (defn sch
Hallo all,
I'm working trough Essentials of Programming Languages. I'm trying to
right a function like this one:
(defn scheme-remove-first [syb lst]
(if (empty? lst)
'()
(if (= (first lst) syb)
(rest lst)
(cons (first lst) (scheme-remove-first syb (rest lst))
in a idiom
19 matches
Mail list logo