Is this a bug in core.match?

55 views
Skip to first unread message

Brian Marick

unread,
Aug 28, 2012, 7:01:22 PM8/28/12
to clo...@googlegroups.com
Observe:

user=> (defn count-sequence [& seq]
(match [seq]
; [([so-far [x & xs]] :seq)] (str "1:" so-far x xs)
[([[& sequence]] :seq)] (str "2:" sequence)))
user=> (count-sequence [1 2 3])
"2:[1 2 3]"

Now uncomment the commented line:

user=> (defn count-sequence [& seq]
(match [seq]
[([so-far [x & xs]] :seq)] (str "1:" so-far x xs)
[([[& sequence]] :seq)] (str "2:" sequence)))
user=> (count-sequence [1 2 3])
nil

This is a bug? Please?

(Note: the same thing happens without a &rest argument if you pass in [[1 2 3]])

-----
Brian Marick, Artisanal Labrador
Contract programming in Ruby and Clojure
Occasional consulting on Agile


Brian Marick

unread,
Aug 28, 2012, 7:16:59 PM8/28/12
to clo...@googlegroups.com
This may also be relevant.

The following works:

(defn factorial [& args]
(match [args]
[([n] :seq)] (factorial 1 n)
[([so-far 1] :seq)] so-far
[([so-far n] :seq)] (factorial (* n so-far) (dec n))))

user=> (factorial 5)
120

However, changing the order of clauses stops that:


(defn factorial [& args]
(match [args]
[([so-far 1] :seq)] so-far
[([so-far n] :seq)] (factorial (* n so-far) (dec n))
[([n] :seq)] (factorial 1 n))) ;; <<== moved this down

user=> (factorial 5)
NullPointerException clojure.lang.Numbers.ops (Numbers.java:942)

David Nolen

unread,
Aug 28, 2012, 9:54:42 PM8/28/12
to clo...@googlegroups.com
There's definitely issues around seq pattern matching. I have a ticket
or two open for this. However the future of seq matching is still
uncertain.

David
Reply all
Reply to author
Forward
0 new messages