Re: I need help! Substrings

John Reppy <[email protected]> Wed, 30 May 2007 15:05:46 -0500
Newsgroups gmane.comp.lang.sml.smlnj
Message-ID <[email protected]>
The first problem is that variables in SML are bound to values, not
locations.  Thus, any reference to "index" in your code is equivalent
to the constant "0".  SML does have reference values, which are mutable
cells, so you can write

	val index = ref 0;

	fun inc x = (x := !x + 1);

	.... inc index ...

Another issue is that since findmatch and checkrest are mutually  
recursive,
they must be defined in the same binding.  The "and" keyword is used for
this purpose:

	fun findmatch (s, l) = ...

	and checkrest (s, l) = ...

Hope this helps.

	- John

On May 30, 2007, at 1:59 PM, turbosol wrote:

>
> this is what I have been able to write so far...
>
>
>
> val index = 0;
>
> fun inc x= x+1;
>
>
> fun findmatch(S,L) =
> if null S then ~1
> else if null L then ~1
> else if hd(S) = hd(L) then checkrest(tl S,tl L)
> else findmatch(S, tl L) + inc(index);
>
> fun checkrest(S, L) =
> if null(tl S) then index
> else if hd(tl S)= hd(tl L) then checkrest(tl S, tl L)
> else findmatch(S, tl L);
>
> fun substring (S,L)= findmatch(explode S,explode L);
>
>
> substring("abc","bdabcd");
>
> ////// however, this does not run, it throws errors about the explode.
> Also, I know that if this was able to compile, it will return the  
> size of
> the list if the string is not found.  It is also only checking for  
> the first
> char in the string S, and does nothing with the rest as of now.
>
>
>
>
>
> turbosol wrote:
>>
>> So i signed up for my last compscience class so i can graduate... the
>> class i needed was canceled so i was put into a more difficult  
>> class for
>> majors.  Comp science is my minor... anyways, I am currently doing  
>> twice
>> the work i would normally do in half the time.  I am lost.  if  
>> anyone can
>> help me, i would appreciate it SOOO much.
>>
>> I need to write a function SubString : string * string -> int that  
>> checks
>> whether the first string is a substring of the second (case  
>> sensitive).
>> return the index position (starting from 0) if the string is a  
>> substring
>> or -1 otherwise.  for multiple occurances, just return the index  
>> of the
>> first appearance.  you may assume both input parameters are strings.
>>
>> subString("ab","abcdefg")=> 1
>> subString("aaa","aaaa")=>0
>> subString("bc","absge") =>-1
>> subString("ab","cdabd") => 2
>>
>
> -- 
> View this message in context: http://www.nabble.com/I-need-help%21-- 
> Substrings-tf3836438.html#a10880352
> Sent from the SML/NJ mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------- 
> ---
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Smlnj-list mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/smlnj-list
>


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/