简体   繁体   中英

Put string in to list in Haskell

I need to create my own words functions. It takes string and puts into list where ever there is space. For example string "i need help" would result in ["i","need","help"]. The definitions must be exactly

anything :: String -> [String]

I currently came up with stupid solution which look like this ( also it doesn't work)

test :: String -> [String]
test d = beforep d : (test (afterp d)) : []

beforep :: String -> String
beforep d = takeWhile (/=' ') d
afterp :: String -> String
afterp d = if (dropWhile (/=' ') d)==[] then []
      else tail(dropWhile (/=' ') d)

test -> uses tail recursion

beforep -> get everything till first space

afterp -> gets everything after space

Any ideas ? If you have any other solutions to this problem it would help. Thank you

You've very nearly got it. If I attempt to run your code as is, I get:

test.hs:2:23:
    Couldn't match expected type `Char' with actual type `String'
    Expected type: String
      Actual type: [String]
    In the return type of a call of `test'
    In the first argument of `(:)', namely `(test (afterp d))'

So examine line 2:

test d = beforep d : (test (afterp d)) : []
--                                      ^
-- This is the problem -----------------|

The type of the cons operator is:

(:) :: a -> [a] -> [a]

Your test function returns a [String] already, you don't want to try to cons it onto an empty list. That would imply that the return type would be [[String]] .

Try this instead:

test d = beforep d : (test (afterp d))

After that change, it compiles, but when you run test "i need help" you get the infinite list:

["i","need","help","","","","","","","",""...

The problem is that you need to include a base case in test that stops when you pass it an empty list. Here's the working code:

test :: String -> [String]
test [] = []
test d = beforep d : (test (afterp d))

beforep :: String -> String
beforep d = takeWhile (/=' ') d

afterp :: String -> String
afterp d = if (dropWhile (/=' ') d)==[]     -- Slightly reformatted
             then []                        -- to improve readability,
             else tail(dropWhile (/=' ') d) -- no real change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM