6 Replies - 2038 Views - Last Post: 26 December 2009 - 04:06 PM

#1 curioComp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-October 09

Second word in a sentence

Post icon  Posted 05 November 2009 - 09:12 AM

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.
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!

Is This A Good Question/Topic? 0
  • +

Replies To: Second word in a sentence

#2 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Second word in a sentence

Posted 06 November 2009 - 11:02 AM

getSecondWord = head . tail . words


*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:

getLastWord = last . words



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



Have fun!
Was This Post Helpful? 0
  • +
  • -

#3 curioComp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-October 09

Re: Second word in a sentence

Posted 12 November 2009 - 11:58 AM

Than you! It was very helpfull, but actually, the third element in my sentences are always numbers, but this:
getLastWord = last . words


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


Thank you so much in advance.
Was This Post Helpful? 0
  • +
  • -

#4 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Second word in a sentence

Posted 12 November 2009 - 06:18 PM

View PostcurioComp, on 12 Nov, 2009 - 10:58 AM, said:

Than you! It was very helpfull, but actually, the third element in my sentences are always numbers, but this:
getLastWord = last . words


It's not good for numbers, just for words...
Is it posible to write something like this?
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.

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! :)

Good luck.

This post has been edited by Raynes: 12 November 2009 - 06:19 PM

Was This Post Helpful? 1

#5 curioComp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-October 09

Re: Second word in a sentence

Posted 26 November 2009 - 09:16 AM

Thank you so much for all your answers!!!

But I really need the last element to be an Int, because afterwards I need to do operations like + or *, and the function getLastWord returns String, and it doesn't let me do operations if the function returns String.

:blink: Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Second word in a sentence

Posted 26 November 2009 - 04:39 PM

View PostcurioComp, on 26 Nov, 2009 - 08:16 AM, said:

Thank you so much for all your answers!!!

But I really need the last element to be an Int, because afterwards I need to do operations like + or *, and the function getLastWord returns String, and it doesn't let me do operations if the function returns String.

:blink: Thanks!


getLastWord :: String -> Int
getLastWord = read . last . words



ghci> getLastWord "a b 2"
2
ghci> getLastWord "a b 2" + 3
5
ghci> getLastWord "word word2 3" + 3
6



You can use the 'read' function to parse a string to a numeric type. The problem with 'read' is that, unless you're using explicit type signatures, or giving the compiler some other way of knowing what numeric type you want out of the parse, it will throw an error, because it wont be able to figure it out. My recommendation is to not remove the type signature from getLastWord, that way you and the compiler both know you're working with Ints.

This post has been edited by Raynes: 26 November 2009 - 04:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 Twey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 26-December 09

Re: Second word in a sentence

Posted 26 December 2009 - 04:06 PM

The (!!) function allows one to retrieve an arbitrary element of a list.

secondWord = (!! 1) . words

thirdWord = (!! 2) . words

-- … and so on…


An Integer or Int can be extracted from a String by using the ‘read’ function.

Be aware, however, that both (!!) and ‘read’ (like ‘head’ and ‘tail’) are unsafe functions: they can error if the provided arguments are improper (longer than the list, empty list, non-parsing integer, &c.). You should check for these cases before using them, unless you're very sure that you will only be provided with sane inputs.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1