Re: [racket-users] Problem with # and mutable lists

2016-04-10 Thread Benjamin Greenman
On Sun, Apr 10, 2016 at 7:29 AM, Joan Martin  wrote:

> I use it without problems : (select-room 2 environment) in this case.
> But when I use it in another procedure (for example: set-symbol),
> sometimes Racket returns this:
>
>   mcdr: contract violation;;(mcdr in let in set-symbol)
>   expected: mpair?
>   given: #;;(returned by select-room ?)
>

This problem may be because your first if-statement in select-room is
missing an else branch.



>
> In Racket documentation I found : " # is returned by most forms and
> procedures that have a side-effect and no useful result."
>

These functions return # because the useful result is usually the
argument you passed the procedure in the first place. The example below
uses set-mcar! to update a list -- the useful result is the original list,
not the result of set-mcar!

#lang racket/base

(define lst (mcons 1 (mcons 2 '(

(displayln lst)
;; (1 2)

(set-mcar! lst 3)

(displayln lst)
;; (3 2)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Problem with # and mutable lists

2016-04-10 Thread Pierpaolo Bernardi
On Sun, Apr 10, 2016 at 1:29 PM, Joan Martin  wrote:
> Hello,
> I try to write a programme (a game), where I use lots of mutable lists (named 
> environment).Something like this:
> (define environment
>  (mcons (mcons  (mcons 1 (mcons (mcons 2(mcons 5(mcons 8 '('() ))
> (mcons  (mcons 2 (mcons (mcons 1(mcons 3(mcons 10 '( '() ))
>   ..
>   '() ))
> It should represent a 2D dekadedron.
> And for easy manipulation I wrote a procedure: select-room and set-symbol.
>
> (define select-room (lambda (number rooms)
>   (let loop ((l (mcar rooms)))
> (if (not (null? l))
> (if (= (mcar(mcar l)) number)
> (mcar l)
> (loop (mcdr l)) )

In Racket this doesn't even compile. So I assume you must be using
some other programming language.

Anyway, what does this function return when (null? l) ?

If you had used Racket, it would have shown you immediately where's the problem.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Problem with # and mutable lists

2016-04-10 Thread Joan Martin
Hello, 
I try to write a programme (a game), where I use lots of mutable lists (named 
environment).Something like this:
(define environment 
 (mcons (mcons  (mcons 1 (mcons (mcons 2(mcons 5(mcons 8 '('() ))
(mcons  (mcons 2 (mcons (mcons 1(mcons 3(mcons 10 '( '() ))
  ..
  '() ))
It should represent a 2D dekadedron.
And for easy manipulation I wrote a procedure: select-room and set-symbol.

(define select-room (lambda (number rooms)
  (let loop ((l (mcar rooms)))
(if (not (null? l))
(if (= (mcar(mcar l)) number)
(mcar l)
(loop (mcdr l)) )
(define set-symbol
  (lambda (symbol number_of_room)
(let ((traps (mcdr(select-room number_of_room environment))  ))
  (set-mcdr! traps (mcons symbol (mcdr traps))  

I use it without problems : (select-room 2 environment) in this case.
But when I use it in another procedure (for example: set-symbol), sometimes 
Racket returns this:

  mcdr: contract violation;;(mcdr in let in set-symbol)
  expected: mpair?
  given: #;;(returned by select-room ?)

In Racket documentation I found : " # is returned by most forms and 
procedures that have a side-effect and no useful result."

But for me it REALLY has useful result. It´s really strange it appears only 
sometimes. And select-room returns a mutable list, so I´m really confused.

I would be glad for any help.

Thanks 
Joan

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.