4 Replies - 371 Views - Last Post: 28 June 2011 - 05:53 PM

#1 therry  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 22
  • Joined: 25-June 11

Understanding if and reply statements in Reacket

Posted 28 June 2011 - 04:52 PM

I X Code X 1,

Could you please explain the following code? I understand the 'if' statements a little, and I understand the 'equal' statement, and I understand the 'substring' statements, However, I am lost with the combination below. I understand that it is a function that is being defined. I do not understand 'reply'.

(define (reply s)
(if (equal? "hello" (substring s 0 5))
"hi"
"huh?"))

>(reply "hello racket")
"hi!"


> (reply "λx:(μα.α→α).xx")
"huh?"

...therry

Is This A Good Question/Topic? 1
  • +

Replies To: Understanding if and reply statements in Reacket

#2 I X Code X 1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: Understanding if and reply statements in Reacket

Posted 28 June 2011 - 05:03 PM

Well this is a first! A topic directed right at me. I think I can help you understand this code:

Let's take it one Step at a time:

First of all, we define a new function with the name reply and one parameter called s. This means when we want to use the reply function we will write this: (reply parameter). Where, in this case, parameter is a string.

Secondly, we use the if statement. If an if statement evaluates to true the first argument is returned "hi", otherwise the second argument is returned "huh?". So let's look at the contents of if:

(if (equal? "hello" (substring s 0 5))


In English this reads, if "hello" is equal to the substring of our parameter s...

Let's say we typed this into the REPL (reply "hello racket")

s = "hello racket"

So, we say (again in English) if "hello" is equal to the substring of "hello racket" from 0 to 5. From your other topic you now know how substring works. Thus, you should easily be able to see that "hi" will be the result. The Substring of "hello racket" from 0 to 5 is "hello". Which is what the if statement is asking for!


Now, if we type something like this: (reply "sup therry"):

s = "sup therry"

If "hello" is equal to the substring of "sup therry" from 0 to 5...this clearly is not the case and the result is "huh?"


Does that help?

This post has been edited by I X Code X 1: 28 June 2011 - 05:32 PM

Was This Post Helpful? 4
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Understanding if and reply statements in Reacket

Posted 28 June 2011 - 05:07 PM

(if condition foo bar) evaluates to foo if condition evaluates to true (which is to say something "truthy") and to bar if it evaluates to false.In your case the condition is (equal? "hello" (substring s 0 5)) and foo and bar are "hi" and "huh?".

(equal? a B) evaluates to #t (i.e. scheme's value for true) if a is equal to b and to #f otherwise. In your case a is "hello" and b is (substring s 0 5).

As you know (substring s 0 5) returns a string containing the first 5 letters of s.

So (if (equal? "hello" (substring s 0 5)) "hi" "huh?") and thus (reply s) evaluates to "hi" if the first 5 letters or s are "hello" and to "huh?" otherwise.

This post has been edited by sepp2k: 28 June 2011 - 05:08 PM

Was This Post Helpful? 3
  • +
  • -

#4 therry  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 22
  • Joined: 25-June 11

Re: Understanding if and reply statements in Reacket

Posted 28 June 2011 - 05:48 PM

Hi you'al,

That helped a lot. IX code XI, You are a great teacher!!! When you explain things there is no room for doubt. I really appreciate that. Seppp2k, thanks for your help....therry
Was This Post Helpful? 1
  • +
  • -

#5 I X Code X 1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: Understanding if and reply statements in Reacket

Posted 28 June 2011 - 05:53 PM

You're very welcome! :smartass:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1