user1150071
user1150071

Reputation: 315

How to use Haskell higher order function foldr to calculate length of a string

I want use Haskell higher order function Foldr to calculate the length of a string

stringlength = foldr (\_n -> 1 + n) 0

it give following error.what is the problem with this code?

Unresolved top-level overloading
*** Binding             : stringlength
*** Outstanding context : (Num b, Num (b -> b))

Upvotes: 1

Views: 820

Answers (1)

hugomg
hugomg

Reputation: 69954

You need to add a space to your pattern matching

(\_ n -> ... )
   ^^here

currently you are matching against a single variable _n instead of against _ and n, as you probably want to.

Upvotes: 8

Related Questions