Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've very nearly got it. If I attempt to run your code as is, I get:</p> <pre><code>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))' </code></pre> <p>So examine line 2:</p> <pre><code>test d = beforep d : (test (afterp d)) : [] -- ^ -- This is the problem -----------------| </code></pre> <p>The type of the cons operator is:</p> <pre><code>(:) :: a -&gt; [a] -&gt; [a] </code></pre> <p>Your <code>test</code> function returns a <code>[String]</code> already, you don't want to try to cons it onto an empty list. That would imply that the return type would be <code>[[String]]</code>.</p> <p>Try this instead:</p> <pre><code>test d = beforep d : (test (afterp d)) </code></pre> <p>After that change, it compiles, but when you run <code>test "i need help"</code> you get the infinite list:</p> <pre><code>["i","need","help","","","","","","","",""... </code></pre> <p>The problem is that you need to include a base case in <code>test</code> that stops when you pass it an empty list. Here's the working code:</p> <pre><code>test :: String -&gt; [String] test [] = [] test d = beforep d : (test (afterp d)) beforep :: String -&gt; String beforep d = takeWhile (/=' ') d afterp :: String -&gt; String afterp d = if (dropWhile (/=' ') d)==[] -- Slightly reformatted then [] -- to improve readability, else tail(dropWhile (/=' ') d) -- no real change. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload