unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Safe pattern matching
@ 2013-02-09  9:57 Nikita Karetnikov
  2013-02-09 10:12 ` Daniel Hartwig
  2013-02-09 10:48 ` Ian Price
  0 siblings, 2 replies; 5+ messages in thread
From: Nikita Karetnikov @ 2013-02-09  9:57 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 581 bytes --]

Any Haskellers here?

How would you rewrite the following function in Guile?

foo :: [Int] -> String -> [Int]
foo (x:y:ys) "+" = (x + y):ys
foo (x:y:ys) "-" = (x - y):ys
foo xs       num = read num:xs

*Main> foo [] "42"
[42]
*Main> foo [1,2] "42"
[42,1,2]
*Main> foo [1,2] "+"
[3]
*Main> foo [1..10] "42"
[42,1,2,3,4,5,6,7,8,9,10]
*Main> foo [1..10] "+"
[3,3,4,5,6,7,8,9,10]

It will fail if you call it like this:

*Main> foo [] "+"
[*** Exception: Prelude.read: no parse

But let's assume that it's OK.

I guess that 'match' is the answer, but I don't understand how to use
it.

[-- Attachment #2: Type: application/pgp-signature, Size: 835 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Safe pattern matching
  2013-02-09  9:57 Safe pattern matching Nikita Karetnikov
@ 2013-02-09 10:12 ` Daniel Hartwig
  2013-02-09 10:15   ` Daniel Hartwig
  2013-02-09 10:48 ` Ian Price
  1 sibling, 1 reply; 5+ messages in thread
From: Daniel Hartwig @ 2013-02-09 10:12 UTC (permalink / raw)
  To: Nikita Karetnikov; +Cc: guile-user

On 9 February 2013 17:57, Nikita Karetnikov <nikita@karetnikov.org> wrote:
> Any Haskellers here?
>
> How would you rewrite the following function in Guile?
>
> foo :: [Int] -> String -> [Int]
> foo (x:y:ys) "+" = (x + y):ys
> foo (x:y:ys) "-" = (x - y):ys
> foo xs       num = read num:xs

Indeed, match can do this.  The style is very similar.

Using symbols and literals, rather than strings:

> (use-modules (ice-9 match))
> (define (foo . args)
    (match args
      (((x y . ys) '+)
       (cons (+ x y) ys))
      (((x y . ys) '-)
       (cons (- x y) ys))
      ((xs num)
       (cons num xs))))
> (foo '() 42)
$1 = (42)
> (foo '(1 2) 42)
$2 = (42 1 2)
> (foo '(1 2) '+)
$3 = (3)
> (use-modules (srfi srfi-1))
> (foo (iota 10 1) 42)
$4 = (42 1 2 3 4 5 6 7 8 9 10)
> (foo (iota 10 1) '+)
$5 = (3 3 4 5 6 7 8 9 10)



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Safe pattern matching
  2013-02-09 10:12 ` Daniel Hartwig
@ 2013-02-09 10:15   ` Daniel Hartwig
  0 siblings, 0 replies; 5+ messages in thread
From: Daniel Hartwig @ 2013-02-09 10:15 UTC (permalink / raw)
  To: Nikita Karetnikov; +Cc: guile-user

On 9 February 2013 18:12, Daniel Hartwig <mandyke@gmail.com> wrote:
> Using symbols and literals, rather than strings:

Though strings work just as well as symbols :-)



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Safe pattern matching
  2013-02-09  9:57 Safe pattern matching Nikita Karetnikov
  2013-02-09 10:12 ` Daniel Hartwig
@ 2013-02-09 10:48 ` Ian Price
  2013-02-09 11:09   ` Ian Price
  1 sibling, 1 reply; 5+ messages in thread
From: Ian Price @ 2013-02-09 10:48 UTC (permalink / raw)
  To: Nikita Karetnikov; +Cc: guile-user

Nikita Karetnikov <nikita@karetnikov.org> writes:

> How would you rewrite the following function in Guile?
>
> foo :: [Int] -> String -> [Int]
> foo (x:y:ys) "+" = (x + y):ys
> foo (x:y:ys) "-" = (x - y):ys
> foo xs       num = read num:xs

Daniel covered most of this already, but instead you might consider
(define foo
  (match-lambda* ....))
rather than
(define (foo . args)
   (match args
     ....))

the nearest analogue to read is object->string, but it is not a generic.

-- 
Ian Price -- shift-reset.com

"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Safe pattern matching
  2013-02-09 10:48 ` Ian Price
@ 2013-02-09 11:09   ` Ian Price
  0 siblings, 0 replies; 5+ messages in thread
From: Ian Price @ 2013-02-09 11:09 UTC (permalink / raw)
  To: Nikita Karetnikov; +Cc: guile-user


> the nearest analogue to read is object->string, but it is not a generic.
That should be string->object. It's funny how often I mix up a->b and b->a

-- 
Ian Price -- shift-reset.com

"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-02-09 11:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-09  9:57 Safe pattern matching Nikita Karetnikov
2013-02-09 10:12 ` Daniel Hartwig
2013-02-09 10:15   ` Daniel Hartwig
2013-02-09 10:48 ` Ian Price
2013-02-09 11:09   ` Ian Price

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).