6 Replies - 3223 Views - Last Post: 07 December 2009 - 04:38 PM

#1 forcaJava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-December 09

Split function in Haskell

Posted 01 December 2009 - 05:35 PM

Hello programming mates!!!

It seems that functional programming is really hard for me ;) I am more into Java!!
I need your help in order to construct a function in Haskell.
Here is what I want:

A function that can handle both to Strings and Lists, which will do the following:

If I write a list of numbers
i.e [13,54,67,34,1,45,67,89,564,1,23,45,65]
and outside the list I write a number then the function has to split the list into a list of lists every time that number occurs!!
i.e

Main> [13,54,67,34,1,45,67,89,564,1,23,45,65] 1
[[13,54,67,34],[45,67,89,564],[23,45,65]]



But the function must also be able to split strings into lists at the letter specified (below space is used)
i.e

Main> "I am the function for splitting strings" ' '
["I","am","the","function","for","splitting","strings"]



Is This A Good Question/Topic? 0
  • +

Replies To: Split function in Haskell

#2 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Split function in Haskell

Posted 01 December 2009 - 06:01 PM

do you have to reinvent the wheel or can you use predefined functions?
Was This Post Helpful? 0
  • +
  • -

#3 forcaJava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-December 09

Re: Split function in Haskell

Posted 01 December 2009 - 06:04 PM

View Postmostyfriedman, on 1 Dec, 2009 - 05:01 PM, said:

do you have to reinvent the wheel or can you use predefined functions?


unfortunately reinvent the wheel!!
Was This Post Helpful? 0
  • +
  • -

#4 forcaJava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-December 09

Re: Split function in Haskell

Posted 06 December 2009 - 02:12 PM

anyone with any ideas?
Was This Post Helpful? 0
  • +
  • -

#5 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Split function in Haskell

Posted 06 December 2009 - 05:31 PM

Try the takeWhile and dropWhile functions.

http://www.haskell.org/hoogle/
Was This Post Helpful? 0
  • +
  • -

#6 forcaJava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-December 09

Re: Split function in Haskell

Posted 07 December 2009 - 06:39 AM

unfortunately they don't do the job...
Was This Post Helpful? 0
  • +
  • -

#7 Raynes   User is offline

  • D.I.C Lover
  • member icon

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

Re: Split function in Haskell

Posted 07 December 2009 - 04:38 PM

myTakeW :: (a -> Bool) -> [a] -> [a]
myTakeW p xs = helper [] xs 
	where helper acc (x:xs) | p x	   = x : helper acc xs
							| otherwise = acc 
		  helper [] _				   = []

myDropW :: (a -> Bool) -> [a] -> [a]
myDropW p xs = helper [] xs
	where helper acc [email protected](x:xs) | p x	   = helper acc xs
							| otherwise = l
		  helper [] _				   = []

myBreak :: (a -> Bool) -> [a] -> ([a],[a])
myBreak p xs = helper [] xs
	where helper acc (x:xs) | p x	   = (reverse acc,x:xs)
							| otherwise = helper (x:acc) xs
		  helper acc _				  = (reverse acc,[])

myWords :: Eq a => [a] -> a -> [[a]]
myWords s p = case myDropW p' s of
			   []  -> []
			   s'  -> w : myWords s'' p 
				   where (w, s'') = myBreak p' s'
	where p' = (== p)



Same functions from my answer in the other question, only I changed it so that it creates the predicate function out of the value you supply it to match instead of having you supply a predicate. I didn't bother changing the names, as I don't expect you to use these functions. Just showing you one way to do it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1