9 Replies - 9208 Views - Last Post: 27 June 2011 - 12:54 PM

#1 therry  Icon User is offline

  • New D.I.C Head

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

Understanding 'substrings' in scheme

Posted 25 June 2011 - 11:45 PM

Hi You'al

I am too new to Scheme to be called a newbie. I need a little help with 'Racket'...understanding substrings:

> (substring "apple" 1 3)
"pp"


> (substring "apple" 1)
"pple"

> (substring "The boy out of the country" 4 7)
"boy"

(define (extract str)
(substring str 4 7))

> (extract "the boy out of the country")
"boy"

> (extract "the country out of the boy")
"cou"

I do not understand where the arg '1', '3', '4', or '7' came from in the above examples, or how they are used.

What instructions are the arg '1 3' in the first example supposed to tell me?

What instructions is the arg '1' in the second example supposed to tell me?

What instructions are the arg '4 7' in the third example supposed to tell me?

what does the arg '4 7' in the 'substring' tell 'extract' what to do?

I've searched in all available reference manuals and all the tutorials I could find...no explanations of what the arg '1', '1 3', or '4 7' do. They use them, but they do not explain how or why....therry

Is This A Good Question/Topic? 1
  • +

Replies To: Understanding 'substrings' in scheme

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Understanding 'substrings' in scheme

Posted 26 June 2011 - 01:00 PM

The first number tells substring at which index the substring should begin. The second number tells it at which index the substring should end. The second number is optional and if it is not given, the substring will go to the end of string.

So when you give the numbers 1 and 3 as arguments, the substring will start at index 1 and go up to index 3 (exclusive). If you only give the number 1, it will go from index 1 to the end. And if you give the numbers 4 and 7 it will go from 4 to 7.

The detailed documentation of the substring function can be found here.
Was This Post Helpful? 1
  • +
  • -

#3 therry  Icon User is offline

  • New D.I.C Head

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

Re: Understanding 'substrings' in scheme

Posted 26 June 2011 - 02:06 PM

Thanks for the reply sepp2k,

Am I correct in understanding that in the first example 'apple 1 3' that the action that is happening is to delete everything except what is between '1' and '3'?

And in the second example 'apple 1' the action is to delete only what is in '1'?

And in the third example (substring "the boy out of the country 4 7") the action is to delete everything except what is between the '4' and the '7'?

In the first example there are no spaces therefore '1' = 'a' and '3' = 'l & e' to be deleted. Is it always a process of deleting?

In the third example (substring "the boy out of the country 4 7") '4' = a 'space' delete all before the '4'. However, '7' = 'y' why is it not deleted?

In the next example (substring "the country out of the boy 4 7") '4' is again a 'space' & '7' = 'u'. Why is it not deleted?

Again, thanks for your patience!!! I am confused.........therry
Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Understanding 'substrings' in scheme

Posted 26 June 2011 - 02:18 PM

Moved to Functional Programming.
Was This Post Helpful? 1
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Understanding 'substrings' in scheme

Posted 26 June 2011 - 02:34 PM

It doesn't delete anything it creates s new string. (This is what functional languages call immutability.)
Was This Post Helpful? 2
  • +
  • -

#6 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 'substrings' in scheme

Posted 26 June 2011 - 02:51 PM

As sepp2k stated:

The function (substring "apple" 1 3) will return the value "pp". Now, since you are new to lisp/racket/scheme, you need to understand some things: what functions do, why substring behaves the way it does and the String data type.

First of all, all functions in any type of lisp will evaluate to a value. This is what is displayed to you when the function is used. Substring returns a string value, which I will discuss soon, unless the parameters are not valid. In which case it will return an error.

Secondly, the 'substring' function is a simple idea that might be a little difficult to understand. 'substring' takes three things, one which is optional. First, it needs you to supply it a string. In our example that sttring is the words enclosed in quotation marks: "apple". Next, it takes a number as the first parameter. This number tells the function where you want the substring to begin. Lastly, 'substring' can also take an optional second parameter that is a number that you wish the substring to end at. If the second parameter is not supplied, 'substring' will take the substring to the end of the given string. Also, note that the second parameter is exclusive, meaning that character is not actually printed. With this being said, you should easily be able to tell why (substring "apple" 1 3) returns the string value "pp".

Lastly, let's have a quick look at the data type string. A string is actually nothing more than an array of characters. Thus, #(#\a #\p #\p #\l #\e) is the exact same thing as "apple". If you've done any kind of programming before you should be aware that arrays start at 0. So, that is why (substring "apple" 1 3) returns "pp" and not "ap".

Also, it is important to note that a space is the exact same thing as a character in a string. So, looking at our array example, let's change it to say "apple pie". Our array would now look like this: #(#\a #\p #\p #\l #\e #\Space #\p #\i #\e). It is treated the same way any other character would be treated.

Hope this helps!

This post has been edited by I X Code X 1: 26 June 2011 - 03:18 PM

Was This Post Helpful? 3
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Understanding 'substrings' in scheme

Posted 26 June 2011 - 02:53 PM

View PostAdamSpeight2008, on 26 June 2011 - 11:34 PM, said:

It doesn't delete anything it creates s new string. (This is what functional languages call immutability.)


Just to be clear: Strings in scheme are perfectly mutable, however substring is indeed a non-mutating function in that it creates a new string and does not affect the original string (as is the case in all languages I know of).
Was This Post Helpful? 2
  • +
  • -

#8 therry  Icon User is offline

  • New D.I.C Head

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

Re: Understanding 'substrings' in scheme

Posted 26 June 2011 - 03:20 PM

Thanks AdamSpeight2008,

That was very helpful! To make sure I understand correctly, the new string is always what is either between the args, or what is left after the first arg.

In "apple 1 3" the new string would be 'pp'. 'pp' is between '1 = a' and '3 = le'.

In 'apple 1' the new string is after 'a' = 'pple'. 'pple' is left over after '1 = a'.

In (substring "the boy out of the country" 4 7), the letters between '4' and '7' = bo. This leads me to wonder why is the answer 'boy' and not just 'bo'? Do spaces count? If spaces do not count, '4' = 'b' and the answer should be 'oy'.

In (substring "the country out of the boy" 4 7),if we count the spaces, '4 = space' and '7 = u'. Between '4' and '7' = 'co'. Why is the answer 'cou'...and not just 'co'? What am I missing? I am still confused...therry
Was This Post Helpful? 1
  • +
  • -

#9 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 'substrings' in scheme

Posted 26 June 2011 - 03:29 PM

Quote

In "apple 1 3" the new string would be 'pp'. 'pp' is between '1 = a' and '3 = le'.

In 'apple 1' the new string is after 'a' = 'pple'. 'pple' is left over after '1 = a'.


That is not quite the way to think about it, though I suppose it does work for that example. '1' is not 'a'. If you look at the response I made earlier, '1 = p' and '3 = l'. The substring's parameters are as such:

1. The first is where it starts. This starts at 0. So in "apple" 0 = a, p = 1, p = 2 .. etc.

2. The second is where is ends. Though this number is exclusive, meaning not included! So, 3 = l, but it's not included.

Thus, the result evaluates to "pp"

Quote

In 'apple 1' the new string is after 'a' = 'pple'. 'pple' is left over after '1 = a'.


This acts in the exact same way as above, but we since there is no second parameter it just takes to the end of the string.

Quote

In (substring "the country out of the boy" 4 7),if we count the spaces, '4 = space' and '7 = u'. Between '4' and '7' = 'co'. Why is the answer 'cou'...and not just 'co'?


The answer is "cou" for the very reason I have mentioned above. Let me break it down:

0 = t
1 = h
2 = e
3 = #\Space
4 = c
5= o
6 = u
7 = n
..etc...

So, we start with 4, which is c. And go up to 7, exclusive. Thus it takes up to spot 6. In result, it evaluates to "cou"

This post has been edited by I X Code X 1: 26 June 2011 - 03:41 PM

Was This Post Helpful? 3
  • +
  • -

#10 therry  Icon User is offline

  • New D.I.C Head

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

Re: Understanding 'substrings' in scheme

Posted 27 June 2011 - 12:54 PM

Hi you'al,

Thanks for the extremely helpful help!!! Substrings are now completely clear. I really appreciate all of you taking the time to sweep the cobwebs from my mind....therry
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1