Mikhail
Mikhail

Reputation: 87

Function that returns a list of words of length n

Hey I am new to Haskell and trying to figure out how I would return a list with words of length n

getWords :: Int -> [Word] -> [Word]
getWords n w = filter (\x -> length x == n) w

I figured out I can use this in Prelude filter (\x -> length x == 5) ["Hello", "23"]

It would return ["Hello"], however when I try to do it in a function getWords it gives me an error

* Couldn't match type `t0 a0' with `Word'
      Expected type: [Word]
        Actual type: [t0 a0]
    * In the expression: filter (\ x -> length x == n) w
      In an equation for `getWords':
          getWords n w = filter (\ x -> length x == n) w
    |
163 | getWords n w = filter (\x -> length x == n) w
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Date.hs:163:45: error:
    * Couldn't match type `Word' with `t0 a0'
      Expected type: [t0 a0]
        Actual type: [Word]
    * In the second argument of `filter', namely `w'
      In the expression: filter (\ x -> length x == n) w
      In an equation for `getWords':
          getWords n w = filter (\ x -> length x == n) w
    |
163 | getWords n w = filter (\x -> length x == n) w

What am I doing wrong here?

Upvotes: 2

Views: 547

Answers (1)

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

What you are doing wrong is using Word presumably from Data.Word. This is a machine word, as in an unsigned integral value. It is not a human word, as in letters from a string. As stated in the comments by @chi, you should use a [String]:

getWords :: Int -> [String] -> [String]
getWords n w = filter (\x -> length x == n) w

Upvotes: 2

Related Questions