take the keywords from a string

Hi everybody, I have a function: takeKeyword :: String -> [String] This function will take the input as a string and return a list of keywords taken from the input string and they are elements of ListOfKeywords. The order of the result list is sequenced as: the last keyword found is set as the first element of the list, and so on. For example, ListOfKeywords = ["expand, "limit", "diff"] takeKeyword :: String -> [String] takeKeyword limit(x + expand((x+3)^2), x=3) = ["expand", "limit"] takeKeyword limit(x -7, x= 3) =["limit"] If you have any suggestions for that, please share with me. Thanks in advance. S.

Hi Sara,
This function will take the input as a string and return a list of keywords taken from the input string and they are elements of ListOfKeywords. The order of the result list is sequenced as: the last keyword found is set as the first element of the list, and so on.
It looks like your language is quite Haskell like, so you can use the built in lex function to split a string into a list. Then a simple filter (`elem` listOfKeywords) will pick out the keywords for you. As for the ordering, maybe you want to apply reverse at the end? Thanks Neil

Sorry, I am not clear at some point in your answer:
1) The function
lex :: String -> [(String,String)]
and
filter :: (a -> Bool) -> [a] -> [a]
So, I did not see how filter can use the list of tuple string of lex.
Sorry if the question seems ridiculous. Thanks.
S.
On 6/17/06, Neil Mitchell
Hi Sara,
This function will take the input as a string and return a list of keywords taken from the input string and they are elements of ListOfKeywords. The order of the result list is sequenced as: the last keyword found is set as the first element of the list, and so on.
It looks like your language is quite Haskell like, so you can use the built in lex function to split a string into a list. Then a simple filter (`elem` listOfKeywords) will pick out the keywords for you.
As for the ordering, maybe you want to apply reverse at the end?
Thanks
Neil

Hi
On 6/18/06, Sara Kenedy
Sorry, I am not clear at some point in your answer:
1) The function lex :: String -> [(String,String)] and filter :: (a -> Bool) -> [a] -> [a] So, I did not see how filter can use the list of tuple string of lex.
You can write a function lexList, of type String -> [String], by repeatedly calling lex - its not too hard. Once you have this the filter will work. Thanks Neil

OK, thank you.
S.
On 6/17/06, Neil Mitchell
Hi
On 6/18/06, Sara Kenedy
wrote: Sorry, I am not clear at some point in your answer:
1) The function lex :: String -> [(String,String)] and filter :: (a -> Bool) -> [a] -> [a] So, I did not see how filter can use the list of tuple string of lex.
You can write a function lexList, of type String -> [String], by repeatedly calling lex - its not too hard. Once you have this the filter will work.
Thanks
Neil

Hi, I tried to write function lexList by using an intermediate function lisOfString as below: module Lex where lexList :: String -> [String] lexList str = listOfString (lex str) lexList [] =[] listOfString :: [(String,String)] -> [String] listOfString [(s1,s2)] = s1: listOfString (lex s2) listOfString [("","")] = [] When I try function lisOfString as below, it runs forever (non-stop) although I have the stop criteria for it ?? Lex> lisOfString ["test1","test2(test3)"] Thanks in advance. S.
On 6/17/06, Neil Mitchell
wrote: Hi
On 6/18/06, Sara Kenedy
wrote: Sorry, I am not clear at some point in your answer:
1) The function lex :: String -> [(String,String)] and filter :: (a -> Bool) -> [a] -> [a] So, I did not see how filter can use the list of tuple string of lex.
You can write a function lexList, of type String -> [String], by repeatedly calling lex - its not too hard. Once you have this the filter will work.
Thanks
Neil

On 6/17/06, Sara Kenedy
When I try function lisOfString as below, it runs forever (non-stop) although I have the stop criteria for it ??
The patterns are tested on a 'first come first served' basis. As your program executes, it tries the first pattern listed, if it matches then the right-hand side of the equation is evaluated. You do have a stop criteria for both functions but it is not evaluating because the pattern that comes before it matches. When using patterns you must remember to put the most specific patterns first or else they may never be reached. I hope that helps, Jason

Thanks, I got it and it run sucessfully now.
On 6/18/06, Jason Dagit
On 6/17/06, Sara Kenedy
wrote: [snip] When I try function lisOfString as below, it runs forever (non-stop) although I have the stop criteria for it ??
The patterns are tested on a 'first come first served' basis. As your program executes, it tries the first pattern listed, if it matches then the right-hand side of the equation is evaluated. You do have a stop criteria for both functions but it is not evaluating because the pattern that comes before it matches. When using patterns you must remember to put the most specific patterns first or else they may never be reached.
I hope that helps, Jason _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello Sara, Sunday, June 18, 2006, 9:04:05 AM, you wrote: you should write stop alternatives first, because Haskell tries alternatives just sequentially, topmost first: listOfString :: [(String,String)] -> [String] listOfString [("","")] = [] listOfString [(s1,s2)] = s1: listOfString (lex s2)
I tried to write function lexList by using an intermediate function lisOfString as below:
module Lex where lexList :: String -> [String] lexList str = listOfString (lex str) lexList [] =[]
listOfString :: [(String,String)] -> [String] listOfString [(s1,s2)] = s1: listOfString (lex s2) listOfString [("","")] = []
When I try function lisOfString as below, it runs forever (non-stop) although I have the stop criteria for it ??
Lex>> lisOfString ["test1","test2(test3)"]
Thanks in advance.
S.
On 6/17/06, Neil Mitchell
wrote: Hi
On 6/18/06, Sara Kenedy
wrote: Sorry, I am not clear at some point in your answer:
1) The function lex :: String -> [(String,String)] and filter :: (a -> Bool) -> [a] -> [a] So, I did not see how filter can use the list of tuple string of lex.
You can write a function lexList, of type String -> [String], by repeatedly calling lex - its not too hard. Once you have this the filter will work.
Thanks
Neil
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (4)
-
Bulat Ziganshin
-
Jason Dagit
-
Neil Mitchell
-
Sara Kenedy