[plt-scheme] A data point on 'match' in intro programming
This is in svn head and tested, but I am pretty sure this worked in
4.0 already.
(Your function definition, if it worked syntactically, would be wrong.)
On Jul 14, 2009, at 11:03 AM, Prabhakar Ragde wrote:
> Matthias Felleisen wrote:
>> Agreed, showing them pattern matching late but still in the first
>> year is the right thing.
>> BSL, with list abbrv:
>>> (require scheme/match)
>>>
>>> ;; [Listof Number] -> Number
>>>
>>> (check-expect (sum '(1 2 3)) 6)
>>>
>>> (define (sum x)
>>> (match x
>>> [`() 0]
>>> [(cons fst rst) (+ fst (sum rst))]))
>> Enjoy -- Matthias
>
> Is this 4.2 where you can (require scheme/match)? I'm still at
> 4.1.3 (waiting for 4.2.1), and I can't. If I put
>
> #lang scheme
> (provide match)
>
> in matchmodule.ss and then (require "matchmodule.ss"), I can get
> your code to work. However, this doesn't work:
>
> (define (sum x)
> (match x
> [`() 0]
> [`(,fst ,rst ...) (+ fst (sum rst))]))
>
> I get:
>
> fst: name is not defined, not a parameter, and not a primitive name
>
> That last bit of code works fine in Module. --PR