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

2016-04-11 Thread Benjamin Greenman
>
> but although I changed else expresion, Racket sometimes returns same
> warning(but now with #f instead of #).


After you call `select-room`, you'll want to check that the result is an
mlist (instead of #f or #) before calling mcdr.

-- 
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] Re: Problem with # and mutable lists

2016-04-10 Thread Joan Martin
Sorry, now with attachment. 

-- 
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.


wumpus 5.rkt
Description: Binary data


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

2016-04-10 Thread Joan Martin
Thank you very much,
but although I changed else expresion, Racket sometimes returns same 
warning(but now with #f instead of #).

But when I click Run again (sometimes twice or more) it returns what I want. 
That´s the strangest thing.

Could it be an error in Racket settings?

I attach my code, maybe I miss someting else.(please ignore auxiliary 
procedures, warning appears while running (set-traps) and of course set-symbol! 
)

-- 
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] Re: Problem with # and mutable lists

2016-04-10 Thread Gustavo Massaccesi
The most literal translation is

#lang racket
(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)))
(void)  ;<-- explicit "else" branch

You can replace (void) with whatever you want to signal that nothing
is found, like 'not-found, but the more idiomatic versions is to
return #f.

(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)))
#f  ;<-- explicit "else" branch

It's not possible to return "nothing", you must return something, like
(void) or #f or 'not-found. When you are going to ignore the result,
it's better to return (void), but if you want to signal that something
is not found it's better to return #f, or raise an error. (Actually,
you can return "nothing", but let's hide that under the carpet for few
months, because it doesn't play nice with most racket code.)

But now set-symbol has to check that traps is not #f:

(define set-symbol!
  (lambda (symbol number_of_room)
(let ((traps (mcdr(select-room number_of_room environment
  (if traps
 (set-mcdr! traps (mcons symbol (mcdr traps)))
 (void ;<-- explicit "else" branch

Here, it's better to return (void) because you are going to ignore the
result, and set-mcdr! returns also (void). The default printer ignore
voids results and prints "nothing", so (void) will be better here.

(I renamed set-symbol as set-symbol!, because the name of most
functions that mutate their argument have bang. It's not a rule, only
a style recommendation.)

A more idiomatic version use when, unless, and and or, but I'm triying
to make the minimal changes to your code.

Gustavo



On Sun, Apr 10, 2016 at 11:30 AM, Joan Martin  wrote:
> Thank for your answer,
> but how should I change the procedure and ged rid of Racket warning?
> I cant figure it out.
>
> --
> 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.

-- 
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] Re: Problem with # and mutable lists

2016-04-10 Thread Joan Martin
Thank for your answer,
but how should I change the procedure and ged rid of Racket warning?
I cant figure it out.

-- 
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] Re: Problem with # and mutable lists

2016-04-10 Thread Joan Martin
Dne neděle 10. dubna 2016 13:29:43 UTC+2 Joan Martin napsal(a):
> 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

I learn Scheme and compiling in Racket with language Pretty Big isn´t problem.

Select-room should be iterative procedure - it goes through list. 
(null? l) tells it is at the end of the list and it returns nothing, because I 
don´t have second else expresion in IF. 


-- 
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.