returning lists

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

returning lists

raptor@tvskat.net
hi,

i'm having this problem, tring to implement split (for learning purposes).
Here is one sample of my tries :

split _ [] = []
split char (x:xs)
        | x /= char = x : split char xs
        | otherwise = []


it is ok i.e. it returns the first of the splits, but I want to get
all of the results.


str = "testingxsplitxtoxseexhowxitxwork"

*Main> split 'x' str
"testing"

I want :
["testing","split",...]


How do i do this.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: returning lists

Ketil Malde-2
raptor <[hidden email]> writes:

> split _ [] = []
> split char (x:xs)
>         | x /= char = x : split char xs
>         | otherwise = []

> *Main> split 'x' str
> "testing"

> I want :
> ["testing","split",...]

> How do i do this.

Collect the read chars in a temp, when the separator is detected,
return the temp (probably reversing it) and recurse.

-k
--
If I haven't seen further, it is by standing in the footprints of giants

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: returning lists

Sebastian Sylvan
In reply to this post by raptor@tvskat.net
On 12/1/05, raptor <[hidden email]> wrote:

> hi,
>
> i'm having this problem, tring to implement split (for learning purposes).
> Here is one sample of my tries :
>
> split _ [] = []
> split char (x:xs)
>         | x /= char = x : split char xs
>         | otherwise = []
>
>
> it is ok i.e. it returns the first of the splits, but I want to get
> all of the results.
>
>
> str = "testingxsplitxtoxseexhowxitxwork"
>
> *Main> split 'x' str
> "testing"
>
> I want :
> ["testing","split",...]
>
>
> How do i do this.
>

A good start is to write a function which returns the first split, as
well as the remainder of the string as a tuple.
Then you can use this function over and over on the remainder of each
call until it is "".

/S
--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: returning lists

raptor@tvskat.net

|
|A good start is to write a function which returns the first split, as
|well as the remainder of the string as a tuple.
|Then you can use this function over and over on the remainder of each
|call until it is "".

]- that is the problem :"), 'cause such functions should accept
Char and String, but return a Tuple of Strings, but it is recursive (so that it
fetch next,next,next char :)) i.e. it is own "consumer" and should return String

--this is wrong, just tring
grab char (x:xs)
        | x /= char = (x : grab char xs, xs)
        | otherwise = ([],[])
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: returning lists

Sebastian Sylvan
On 12/1/05, raptor <[hidden email]> wrote:

>
> |
> |A good start is to write a function which returns the first split, as
> |well as the remainder of the string as a tuple.
> |Then you can use this function over and over on the remainder of each
> |call until it is "".
>
> ]- that is the problem :"), 'cause such functions should accept
> Char and String, but return a Tuple of Strings, but it is recursive (so that it
> fetch next,next,next char :)) i.e. it is own "consumer" and should return String
>
> --this is wrong, just tring
> grab char (x:xs)
>         | x /= char = (x : grab char xs, xs)
>         | otherwise = ([],[])

I'm not going to give you a solution, since I suspect this is homework.

But consider this hypothetical (and utterly nonsensical) function

foo :: [Int] -> ([Int],[Int])
foo (x:xs)
  | x < 5 = ([x] , ys)
  | otherwise ( 5 : ys, rest)
  where (ys, rest) = foo xs

I mean, this function doesn't do anything useful at all, so don't
bother trying to figure out what it does :-)
But the point is that you can still use recursion even if the result
of the function is a tuple. Just put the recursive call in a
where-statement (it won't get evaluated unless needed, laziness buys
us that), and pattern match against the contents of this tuple.

Of course, this can be written using the higher order functions
dropWhile and takeWhile, as well, but maybe you're required to write
it from scratch?


/S


--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: returning lists

Chad Scherrer-2
In reply to this post by raptor@tvskat.net
raptor <[hidden email]> writes,

> ]- that is the problem :"), 'cause such functions should accept Char and
> String, but return a Tuple of Strings, but it is recursive (so that it fetch
> next,next,next char :)) i.e. it is own "consumer" and should return String

> --this is wrong, just tring grab char (x:xs)

You might also have a look at
http://haskell.org/hawiki/WorkerWrapper

This idea should apply here.

-Chad Scherrer
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe