简体   繁体   中英

Define Haskell function with let fun x = x + 1 form

If I have a function as following:

f::[a]->Integer
f  [] = 0
f (x:cx) = 1 + (f cx)

how can I define the function inside the main with 'let'?

I know I can define a function inside main: eg

let f x = x + 1

but how I'm pattern-matching for "f [] = 0" ?

Thanks

You can still do it the same way:

main =
    let f  [] = 0
        f (x:cx) = 1 + (f cx)
    in
    print (f "abc")

or

main =
    let
        f  [] = 0
        f (x:cx) = 1 + (f cx)
    in
    print (f "abc")

The crucial part is that the f s in the two equations must line up and be indented more than let .

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