functional programming - Haskell, Don't know why this has a *parse error on input ‘if’* -
this take number, factorial , double it, because of base of operations case if input 0 gives 2 reply in order bypass used if statement, error parse error on input ‘if’. appreciate if guys help :)
fact :: int -> int fact 0 = 1 fact n = n * fact(n-1) doub :: int -> int doub r = 2 * r factorialdouble :: io() factorialdouble = putstr "enter value: " x <- getline allow num = (read x) :: int if (num == 0) error "factorial of 0 0" else allow y = doub (fact num) putstrln ("the double of factorial of " ++ x ++ " " ++ (show y))
i've spotted 2 issues should addressed
you havelet
has no continuation: (else allow y = doub (fact num) ...
). because you're not within do
, want alter let ... in
statement. your if
indented far in. should under let
. i've corrected mentioned , code works me...
fact :: int -> int fact 0 = 1 fact n = n * fact(n-1) doub :: int -> int doub r = 2 * r factorialdouble :: io () factorialdouble = putstr "enter value: " x <- getline allow num = (read x) :: int if num == 0 (error "factorial of 0 0") else allow y = doub (fact num) in putstrln ("the double of factorial of " ++ x ++ " " ++ (show y))
haskell functional-programming
No comments:
Post a Comment