remove first?

2014-04-02 Thread Christopher Howard
Is there some like remove, but only removing the first item found? Not exactly an incredibly hard problem, but with all this clojure stuff about collections and sequences, I'm not quite sure what the appropriate way is to implement it. -- You received this message because you are subscribed to

Re: remove first?

2014-04-02 Thread Timothy Baldridge
I don't know of anything built-in, but this should do the trick: (defn remove-first [f [head tail]] (if (f head) tail (cons head (lazy-seq (remove-first f tail) Timothy On Wed, Apr 2, 2014 at 1:44 PM, Christopher Howard cmhowa...@alaska.eduwrote: Is there some like remove

Re: remove first?

2014-04-02 Thread Christopher Howard
On Wed, 2 Apr 2014 14:07:47 -0600 Timothy Baldridge tbaldri...@gmail.com wrote: I don't know of anything built-in, but this should do the trick: (defn remove-first [f [head tail]] (if (f head) tail (cons head (lazy-seq (remove-first f tail) Timothy Thanks

Re: remove first?

2014-04-02 Thread A. Webb
On Wednesday, April 2, 2014 3:39:47 PM UTC-5, Christopher Howard wrote: On Wed, 2 Apr 2014 14:07:47 -0600 Timothy Baldridge tbald...@gmail.com javascript: wrote: I don't know of anything built-in, but this should do the trick: (defn remove-first [f [head tail]] (if (f head

Re: remove first?

2014-04-02 Thread Chris Lappe
Wouldn't just calling rest on your collection do what you want? On Wed, Apr 2, 2014 at 1:07 PM, Timothy Baldridge tbaldri...@gmail.comwrote: I don't know of anything built-in, but this should do the trick: (defn remove-first [f [head tail]] (if (f head) tail (cons head

Re: remove first?

2014-04-02 Thread Ben Wolfson
On Wed, Apr 2, 2014 at 1:36 PM, Chris Lappe chris.la...@gmail.com wrote: Wouldn't just calling rest on your collection do what you want? (remove-first #(= % 3) [1 2 3 4 3 5]) should return [1 2 4 3 5]. -- Ben Wolfson Human kind has used its intelligence to vary the flavour of drinks, which

Re: remove first?

2014-04-02 Thread Alan Forrester
, 2014 at 1:07 PM, Timothy Baldridge tbaldri...@gmail.com wrote: I don't know of anything built-in, but this should do the trick: (defn remove-first [f [head tail]] (if (f head) tail (cons head (lazy-seq (remove-first f tail) Timothy On Wed, Apr 2, 2014 at 1:44 PM

Re: Remove-first function

2010-07-26 Thread Mark Engelberg
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

Re: Remove-first function

2010-07-25 Thread nickikt
places to place the call to lazy-seq.  You can put lazy-seq around the full body of the function.  You can place it around the cons.  You can place it around the recursive call to scheme-remove-first.  Each choice results in slightly different laziness behavior, i.e., when various elements

Re: Remove-first function

2010-07-25 Thread Gary Fredericks
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

Re: Remove-first function

2010-07-25 Thread Mark Engelberg
On Sat, Jul 24, 2010 at 9:07 AM, Gary Fredericks fredericksg...@gmail.com wrote: (defn remove-first   [syb lst]   (let [[before after]   (loop [b [] a lst]     (if (empty? lst)   [b a]   (if (= syb (first a))     [b (rest

Remove-first function

2010-07-24 Thread nickikt
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

Re: Remove-first function

2010-07-24 Thread Randy Hudson
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 nick...@gmail.com wrote: Hallo all, I'm working trough Essentials of Programming Languages. I'm trying to right a function like this one

Re: Remove-first function

2010-07-24 Thread Andrew Boekhoff
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

Re: Remove-first function

2010-07-24 Thread Mark Engelberg
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 nick...@gmail.com wrote: (defn scheme-remove-first [syb lst]  (if (empty? lst)    '()    (if (= (first lst) syb)      (rest lst)      (cons (first lst

Re: Remove-first function

2010-07-24 Thread Mark Engelberg
around the full body of the function. You can place it around the cons. You can place it around the recursive call to scheme-remove-first. Each choice results in slightly different laziness behavior, i.e., when various elements are computed, but the overall semantics of the sequence remains the same

Re: Remove-first function

2010-07-24 Thread ataggart
it around the recursive call to scheme-remove-first.  Each choice results in slightly different laziness behavior, i.e., when various elements are computed, but the overall semantics of the sequence remains the same and stack overflows will be avoided.  Placing the lazy-seq around the recursive function