English 中文(简体)
Why is this Haskell incorrect?
原标题:

I have a Haskell file which looks like this:

longest::[Integer]->[Integer]->[Integer]
max a b = if length a > length b then a else b

llfs::[Integer]->Integer
llfs li = length(foldl longest group(li))

llfs([1, 2, 3, 3, 4, 5, 1, 1, 1])

Which should print out the result of the function call at the end, however when I run the file I get this error:

parse error (possibly incorrect indentation)

I don t understand what I m doing wrong. What should I do to fix it?


Edit

After putting the last line inside the main function, like this:

import List

longest::[Integer]->[Integer]->[Integer]
longest a b = if length a > length b then a else b

llfs::[Integer]->Integer
llfs li = length(foldl longest group(li))

main = print (llfs [1, 2, 3, 3, 4, 5, 1, 1, 1])

I now get the following error:

C:UsersMartinDesktopHaskellQ1.hs:7:31:
    Couldn t match expected type `[Integer] 
           against inferred type `[a] -> [[a]] 
    In the second argument of `foldl , namely `group 
    In the first argument of `length , namely
        `(foldl longest group (li)) 
    In the expression: length (foldl longest group (li))

This one looks a little more difficult! How do I solve it?

最佳回答
module Main where

import Data.List

longest::[Integer]->[Integer]->[Integer]
longest a b = if length a > length b then a else b

llfs::[Integer]->Int
llfs li = length $ foldl1 longest $ group li

main = do
    putStrLn $ show $ llfs [1, 2, 3, 3, 4, 5, 1, 1, 1]
问题回答

Your code isn t correct.

This

longest::[Integer]->[Integer]->[Integer]
max a b = if length a > length b then a else b

should be

longest::[Integer]->[Integer]->[Integer]
longest a b = if length a > length b then a else b

And you need a main function

main = do print llfs([1, 2, 3, 3, 4, 5, 1, 1, 1])

Just for the sake of improving your code, if you have a function signature and give it a lower case letter(s) as its type (say the letter a), it becomes generic. For example

longest:: [a] -> [a] -> [a]
longest x y = if length x > length y then x else y

Means that rather than just working on lists of Integers, it works on lists of anything. Suddenly you have a very reusable function.

In the line

llfs li = length(foldl longest group(li))

the interpreter is treating group as the second argument to foldl. Writing group(li) is no different from writing group li.

Also, foldl needs an initial value. foldl1, on the other hand, uses the first list element for its initial value. Try:

llfs li = length (foldl1 longest (group li))

(Edited to remove the first, wrong answer.)

The problem was that the last line did not define a function, as others have stated. More things are wrong in your code. It appears this is what you want to do:

import Data.List

longest_group_size :: [Integer] -> Int
longest_group_size = maximum . map length . group

main :: IO ()
main = print $ longest_group_size [1, 2, 3, 3, 4, 5, 1, 1, 1]

Observe that:

  1. You need to import Data.List in order to use group.
  2. No need to use foldr in this case: by using map the length of each group is only calculated once.
  3. This does mean, of course, that we call in the help of another function, maximum.

You cannot call a function at file scope like you would do in python or other scripting languages. Therefore the "call" to llfs in the last line is an error. Try printing the result in main:

main = print (llfs [1, 2, 3, 3, 4, 5, 1, 1, 1])

At the moment the "function call" looks like an incomplete function definition, where the right side is missing, which leads to the surprising error message:

llfs (...) = abc

The problem is this line:

llfs([1, 2, 3, 3, 4, 5, 1, 1, 1])

That s not a function declaration. I think you re trying to make a function call, in which case you need to put it inside a main declaration. You can also load the Haskell file into an interpreter (e.g., ghci) and execute the function call in the interpreter console.

This isn t the direct cause of either error, but I think it s a contributing factor to your misunderstanding. In Haskell, you would never write group(li). Parenthesizing a single argument is pointless — it s exactly equivalent to group li. If you re trying to pass the result of this function call to another function, you need to parenthesize the whole expression — (group li) — or use the $ operator like Caleb suggested.

Two small issues with the update. First, it looks like you re trying to pass the group result as an argument to foldl. The right way to say that is (group li) rather than group(li) The second is that foldl needs a base case. Caleb s suggestion to use foldl1 is one option that will probably work for you.





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

pdo database abstraction

Can someone help me to see what is going wrong with this setup I build the @sql query in the function below like this. The extra quotes are setup in the conditions array. $sql .= " WHERE $...

I wish I could correlate an "inline view"

I have a Patient table: PatientId Admitted --------- --------------- 1 d/m/yy hh:mm:ss 2 d/m/yy hh:mm:ss 3 d/m/yy hh:mm:ss I have a PatientMeasurement table (0 to ...

Syntax help! Php and MYSQL

Original: $sql = "SELECT DATE(TimeAdded) AS Date, $column_name FROM Codes ORDER BY TimeAdded ASC"; Altered: $sql = "SELECT DATE("m", TimeAdded ) AS Date, ColumnName FROM TableName ORDER BY ...

Is this code Equivalent

I am not a fan of the following construction if (self = [super init]) { //do something with self assuming it has been created } Is the following equivalent? self = [super init]; if (self != ...

热门标签