# Understanding if and reply statements in Reacket

Page 1 of 1

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

### #1 therry

Reputation: 8
• 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'.

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

"hi!"

"huh?"

...therry

Is This A Good Question/Topic? 1

## Replies To: Understanding if and reply statements in Reacket

### #2 I X Code X 1

Reputation: 47
• 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

### #3 sepp2k

• D.I.C Lover

Reputation: 2296
• Posts: 3,551
• 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 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

### #4 therry

Reputation: 8
• 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

### #5 I X Code X 1

Reputation: 47
• 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!