Re: [racket-users] Help understanding cond expression

2019-01-15 Thread Michael Murdock MacLeod
On Saturday, January 12, 2019 8:34:35 PM PST Hassan Shahin wrote:
> I have this definition for a procedure:
> 
> (define type-of (lambda (item)
>  (cond
>[(pair? item) 'pair]
>[(null? item) 'empty-list]
>[(number? item) 'number]
>[(symbol? item) 'symbol]
>[else 'some-other-type])))
> 
> My understanding is that if the first 4 conditions fail (=> #f
> ), then the last expression (the else
> expression) is evaluated.
> When I apply this procedure to John, as in (type-of John) I get an error
> (; john: undefined; ; cannot reference an identifier before its definition)
> .
> 
> What is going on?
> Thanks

The issue is that John is evaluated before it is passed to type-of. For 
example, the expression (type-of (+ 2 3)) is equivalent to (type-of 5), 
because the expression (+ 2 3) is evaluated to 5 before it is supplied to the 
type-of function.

In the Google+ post you write "cond behaves as if (not evaluating its 
arguments)". It is true that the arguments to cond, i.e. [(pair? item) 'pair], 
[(null? item) 'empty-list], and so on are not immediately evaluated. However, 
item is evaluated before it is passed to type-of, hence the "cannot reference" 
error.

See https://docs.racket-lang.org/reference/if.html and 
https://docs.racket-lang.org/reference/eval-model.html?q=evaluation%20model for 
more information 
about how Racket evaluates expressions.


-- 
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] Help understanding cond expression

2019-01-12 Thread Hassan Shahin
Thanks Jon.

yes. I did, and you are right. I posted the same question a Google+ forum 
(https://plus.google.com/108613325307702875646/posts/1BaDJFoat4D), and I 
also got a reply that:
"John is being parsed as a variable, and thus, its binding is looked up in 
the local environment. Of course, there is none, hence the error."

- Hassan

On Sunday, January 13, 2019 at 7:52:53 AM UTC+2, Jon Zeppieri wrote:
>
>
>
> On Sun, Jan 13, 2019 at 12:37 AM Hassan Shahin  > wrote:
>
>> Thanks Jack and Mike!
>>
>> You are right. Arguments to procedures will be evaluated before the 
>> invocation of the procedure. 
>>
>
> This is true, but it's not really the issue in your case. Even in #lang 
> lazy, which does not eagerly evaluate procedure arguments, your program 
> would still be an error, because the expression `John` has no meaning at 
> all unless you define it. Try the following in both #lang racket and #lang 
> lazy:
>
> --- definitions window ---
> #lang lazy ;; or racket
>
> (define type-of (lambda (item other-message)
>   (cond
> [(pair? item) 'pair]
> [(null? item) 'empty-list]
> [(number? item) 'number]
> [(symbol? item) 'symbol]
> [else
>  other-message
>  'some-other-type])))
>
> --- interactions window ---
> (type-of #\e (displayln "hello"))
> (type-of (cons 2 3) (displayln "hello"))
> (type-of John (displayln "hello"))
>
> ---
>
> The `John` example will fail in both languages, but you'll observe the 
> difference with the other two examples.
>
> - Jon
>
>
>
>

-- 
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] Help understanding cond expression

2019-01-12 Thread Jon Zeppieri
On Sun, Jan 13, 2019 at 12:37 AM Hassan Shahin  wrote:

> Thanks Jack and Mike!
>
> You are right. Arguments to procedures will be evaluated before the
> invocation of the procedure.
>

This is true, but it's not really the issue in your case. Even in #lang
lazy, which does not eagerly evaluate procedure arguments, your program
would still be an error, because the expression `John` has no meaning at
all unless you define it. Try the following in both #lang racket and #lang
lazy:

--- definitions window ---
#lang lazy ;; or racket

(define type-of (lambda (item other-message)
  (cond
[(pair? item) 'pair]
[(null? item) 'empty-list]
[(number? item) 'number]
[(symbol? item) 'symbol]
[else
 other-message
 'some-other-type])))

--- interactions window ---
(type-of #\e (displayln "hello"))
(type-of (cons 2 3) (displayln "hello"))
(type-of John (displayln "hello"))

---

The `John` example will fail in both languages, but you'll observe the
difference with the other two examples.

- Jon

-- 
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] Help understanding cond expression

2019-01-12 Thread Hassan Shahin
Thanks Jack and Mike!

You are right. Arguments to procedures will be evaluated before the 
invocation of the procedure. I thought that because (if) is not an ordinary 
procedure, and because one can express if in terms of cond (or vice versa) 
that my procedure is also a non ordinary procedure, which is not the case.

Thanks again

On Sunday, January 13, 2019 at 7:29:10 AM UTC+2, Jack Rosenthal wrote:
>
> On Sat, 12 Jan 2019 at 21:12 -0800, Hassan Shahin wrote: 
> > When I apply the procedure to 'John it will evaluate to 'symbol. The 
> idea 
> > of the procedure is to check the "type" of the given item, which is not 
> > decided aprior. If I give it 'John I know it is a symbol. 
> > May be my question should be formulated as this: Since John is not a 
> pair, 
> > an empty-list, a number, not a symbol, why the execution doesn't proceed 
> to 
> > the else expression no matter what John is, unless you are telling me 
> that 
> > the item given to the procedure is evaluated first. 
>
> Perhaps you meant to define John before: 
>
> (define John '(1 2 3)) 
> (type-of John) => 'pair 
>
> Since type-of is a procedure, this means that when it is applied, it's 
> arguments will be evaluated during application. Evaluating a symbol 
> which is not defined will result in an error. 
>
> -- 
> Jack M. Rosenthal 
> http://jack.rosenth.al 
>
> A virtual filesystem? I don't know what you are trying to achieve, 
> but there's probably a better way. 
> -- Me 
>
>

-- 
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] Help understanding cond expression

2019-01-12 Thread Jack Rosenthal
On Sat, 12 Jan 2019 at 21:12 -0800, Hassan Shahin wrote:
> When I apply the procedure to 'John it will evaluate to 'symbol. The idea 
> of the procedure is to check the "type" of the given item, which is not 
> decided aprior. If I give it 'John I know it is a symbol.
> May be my question should be formulated as this: Since John is not a pair, 
> an empty-list, a number, not a symbol, why the execution doesn't proceed to 
> the else expression no matter what John is, unless you are telling me that
> the item given to the procedure is evaluated first.

Perhaps you meant to define John before:

(define John '(1 2 3))
(type-of John) => 'pair

Since type-of is a procedure, this means that when it is applied, it's
arguments will be evaluated during application. Evaluating a symbol
which is not defined will result in an error.

-- 
Jack M. Rosenthal
http://jack.rosenth.al

A virtual filesystem? I don't know what you are trying to achieve,
but there's probably a better way.
-- Me

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


signature.asc
Description: PGP signature


Re: [racket-users] Help understanding cond expression

2019-01-12 Thread Hassan Shahin
Thanks Mike.

When I apply the procedure to 'John it will evaluate to 'symbol. The idea 
of the procedure is to check the "type" of the given item, which is not 
decided aprior. If I give it 'John I know it is a symbol.
May be my question should be formulated as this: Since John is not a pair, 
an empty-list, a number, not a symbol, why the execution doesn't proceed to 
the else expression no matter what John is, unless you are telling me that 
the item given to the procedure is evaluated first.


On Sunday, January 13, 2019 at 6:58:45 AM UTC+2, Mike MacHenry wrote:
>
> You need to apply the function to 'John, with a single quote in front of 
> it. The word John without that quote is just a variable reference to 
> something that you have not actually defined. 
>
> On Sat, Jan 12, 2019 at 11:34 PM Hassan Shahin  > wrote:
>
>> I have this definition for a procedure:
>>
>> (define type-of (lambda (item)
>>  (cond
>>[(pair? item) 'pair]
>>[(null? item) 'empty-list]
>>[(number? item) 'number]
>>[(symbol? item) 'symbol]
>>[else 'some-other-type])))
>>
>> My understanding is that if the first 4 conditions fail (=> #f 
>> ), then the last expression (the 
>> else expression) is evaluated. 
>> When I apply this procedure to John, as in (type-of John) I get an error 
>> (; john: undefined; ; cannot reference an identifier before its 
>> definition). 
>>
>> What is going on?
>> Thanks
>>
>> -- 
>> 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...@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.


Re: [racket-users] Help understanding cond expression

2019-01-12 Thread Mike MacHenry
You need to apply the function to 'John, with a single quote in front of
it. The word John without that quote is just a variable reference to
something that you have not actually defined.

On Sat, Jan 12, 2019 at 11:34 PM Hassan Shahin  wrote:

> I have this definition for a procedure:
>
> (define type-of (lambda (item)
>  (cond
>[(pair? item) 'pair]
>[(null? item) 'empty-list]
>[(number? item) 'number]
>[(symbol? item) 'symbol]
>[else 'some-other-type])))
>
> My understanding is that if the first 4 conditions fail (=> #f
> ), then the last expression (the
> else expression) is evaluated.
> When I apply this procedure to John, as in (type-of John) I get an error
> (; john: undefined; ; cannot reference an identifier before its
> definition).
>
> What is going on?
> Thanks
>
> --
> 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] Help understanding cond expression

2019-01-12 Thread Hassan Shahin
I have this definition for a procedure:

(define type-of (lambda (item)
 (cond
   [(pair? item) 'pair]
   [(null? item) 'empty-list]
   [(number? item) 'number]
   [(symbol? item) 'symbol]
   [else 'some-other-type])))

My understanding is that if the first 4 conditions fail (=> #f 
), then the last expression (the else 
expression) is evaluated. 
When I apply this procedure to John, as in (type-of John) I get an error 
(; john: undefined; ; cannot reference an identifier before its definition)
. 

What is going on?
Thanks

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