If you really want to have _exactly_ what Haskell does (derives the range
from an example) -- yes, you could do a macro. Clojure's new spec facility
(in 1.9-alphas) makes the macro arguments checked at load time too, which
is extra nice:
(set! *print-length* 5) ;; avoid printing infinite lists
(defmacro haskell-like-range [lower next dots]
`(range ~lower Long/MAX_VALUE ~(- next lower)))
(require '[clojure.spec :as s])
(s/fdef haskell-like-range :args (s/cat :lower int? :next int? :dots
'#{...}))
;; these will fail at load time with a really nice error message
(haskell-like-range :foo 42 ...)
(haskell-like-range 4 6 ..) ;; needs three dots according to the spec
;; this will succeed
(haskell-like-range 2 6 ...)
On Wednesday, December 14, 2016 at 1:02:38 AM UTC-5, tbc++ wrote:
>
> Ah! I knew there was a way to do it via iterate, but it was 9:30pm at the
> time and I was tired. Nice example though.
>
> On Tue, Dec 13, 2016 at 10:03 PM, Ghadi Shayban <[email protected]
> <javascript:>> wrote:
>
>> A common way to do it in Clojure is `iterate`, no macros necessary. As of
>> Clojure 1.7 this doesn't allocate a list at all if you reduce/fold over it:
>> (iterate #(+ % 2) 4)
>>
>>
>> On Tuesday, December 13, 2016 at 11:27:28 PM UTC-5, tbc++ wrote:
>>>
>>> I'm not aware of such a construct, but it's trivial enough to write
>>> something like this using `range` or perhaps write a function that will
>>> yield a lazy seq:
>>>
>>> (defn inf-list
>>> ([x y]
>>> (cons x (cons y (inf-list x y 2))))
>>> ([x y c]
>>> (cons (+ x (* c (- y x)))
>>> (lazy-seq (inf-list x y (inc c))))))
>>>
>>> No macros required
>>>
>>> On Tue, Dec 13, 2016 at 9:14 PM, bill nom nom <[email protected]>
>>> wrote:
>>>
>>>> In Haskell, I can get an infinite lazy list that is incremented by two
>>>> by, starting at 4
>>>> [4,6..]
>>>>
>>>> which yields the result
>>>> [4,6,8,10...]
>>>>
>>>> the more general form
>>>> [x,y..] produces [x, x + (y-x), x + 2 * (y-x), x + 3 * (y-x)...]
>>>>
>>>> Is there a way to make a macro of this in clojure, or is there
>>>> something already as elegant?
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Clojure" group.
>>>> To post to this group, send email to [email protected]
>>>> Note that posts from new members are moderated - please be patient with
>>>> your first post.
>>>> To unsubscribe from this group, send email to
>>>> [email protected]
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/clojure?hl=en
>>>> ---
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Clojure" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>>
>>>
>>> --
>>> “One of the main causes of the fall of the Roman Empire was that–lacking
>>> zero–they had no way to indicate successful termination of their C
>>> programs.”
>>> (Robert Firth)
>>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to [email protected]
>> <javascript:>
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> [email protected] <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> “One of the main causes of the fall of the Roman Empire was that–lacking
> zero–they had no way to indicate successful termination of their C
> programs.”
> (Robert Firth)
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.