School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,125 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,042 people online right now. Registration is fast and FREE... Join Now!




Second word in a sentence

 

Second word in a sentence

curioComp

5 Nov, 2009 - 08:12 AM
Post #1

New D.I.C Head
*

Joined: 25 Oct, 2009
Posts: 5

I'm trying to write a function that gives me the second word in a sentence, what I have is getword - but it gives me only the first word, and I don't know what to change to get only the second word from the sentece/line in a file.
CODE

getword :: String -> String
getword "" = []
getword (x:xs)
| (x == ' ') = []
| otherwise = [x] ++ (getword xs)

All my sentences are like this: push constant 10
Can I use the same thing to get the third element that is a number?
Thank you!

User is offlineProfile CardPM
+Quote Post


Raynes

RE: Second Word In A Sentence

6 Nov, 2009 - 10:02 AM
Post #2

Resident Witch. No, really.
Group Icon

Joined: 5 Jan, 2009
Posts: 957



Thanked: 14 times
Dream Kudos: 250
My Contributions
CODE
getSecondWord = head . tail . words


CODE

*Main> getSecondWord "I smell Haskell on your breath."
"smell"


To get the third element (which appears to be the last element in each of your sentences, you would do something like this:

CODE

getLastWord = last . words


CODE

*Main> getLastWord "I smell Haskell on your breath."
"breath."


Have fun!
User is offlineProfile CardPM
+Quote Post

curioComp

RE: Second Word In A Sentence

12 Nov, 2009 - 10:58 AM
Post #3

New D.I.C Head
*

Joined: 25 Oct, 2009
Posts: 5

Than you! It was very helpfull, but actually, the third element in my sentences are always numbers, but this:
CODE

getLastWord = last . words

It's not good for numbers, just for words...
Is it posible to write something like this?
CODE

getLastWord = last . numbers

Thank you so much in advance.

User is offlineProfile CardPM
+Quote Post

Raynes

RE: Second Word In A Sentence

12 Nov, 2009 - 05:18 PM
Post #4

Resident Witch. No, really.
Group Icon

Joined: 5 Jan, 2009
Posts: 957



Thanked: 14 times
Dream Kudos: 250
My Contributions
QUOTE(curioComp @ 12 Nov, 2009 - 10:58 AM) *

Than you! It was very helpfull, but actually, the third element in my sentences are always numbers, but this:
CODE

getLastWord = last . words

It's not good for numbers, just for words...
Is it posible to write something like this?
CODE

getLastWord = last . numbers

Thank you so much in advance.


As long as the number is separated from the second and forth word by a space, like so: "word1 word2 3 word4", the 'words' function will work. What the 'words' function does is split up a string at every space, and eliminates the space. The word function will work just fine for you.

CODE

words "word1 word2 3 word4 word5" -- This returns ["word1","word2","3","word4","word5"]


On an unrelated note, I would really appreciate it if you could click the "this post was helpful" link on any of my posts that you find helpful. You don't have to, but it would be really awesome if you did! smile.gif

Good luck.

This post has been edited by Raynes: 12 Nov, 2009 - 05:19 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:17PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month