3 ms·
Wow what a lot of parentheses! To me the first example is perfectly clear because it follows the same precedence rules I learned for arithmetic at school, along
by RobMurray 2y ago
Wow what a lot of parentheses! To me the first example is perfectly clear because it follows the same precedence rules I learned for arithmetic at school, along with most programming languages. There's nothing special about Haskell here, in Python I would write:
>>> math.sqrt(2) + 1 * 3 + 3 * 2 + 1 / 7
10.557070705230238
The only difference I see is the parentheses used for function application in python.
- indigosun 2y agoYou should see when negative numbers are involved. Due to Haskell's rigor in regard negative numbers: ghci> -1 + 2 1 ghci> 2 + -1 <interactive>:12:1: error: Precedence parsing error cannot mix ‘+’ [infixl 6] and prefix `-' [infixl 6] in the same infix expression ghci> 2 + (-1) 1 ghci> -1 * 3 -3 ghci> 3^-1 <interactive>:3:2: error: • Variable not in scope: (^-) :: t0 -> t1 -> t • Perhaps you meant one of these: ‘^’ (imported from Prelude), ‘-’ (imported from Prelude), ‘^^’ (imported from Prelude) ghci> 3^^-1 <interactive>:4:2: error: • Variable not in scope: (^^-) :: t0 -> t1 -> t • Perhaps you meant ‘^^’ (imported from Prelude) ghci> 3**-1 <interactive>:5:2: error: • Variable not in scope: (**-) :: t0 -> t1 -> t • Perhaps you meant ‘**’ (imported from Prelude) ghci> 3**(-1) 0.3333333333333333 ghci> sqrt 2 + 1 * 3 + 3 * 2 + 1 / 7 10.557070705230238 ghci> sqrt $ 2 + 1 * 3 + 3 * 2 + 1 / 7 3.3380918415851206 ghci> -sqrt 2 + 1 * 3 + 3 * 2 + 1 / 7 7.728643580484048 ghci> -sqrt $ 2 + 1 * 3 + 3 * 2 + 1 / 7 <interactive>:25:1: error: • Non type-variable argument in the constraint: Num (a -> a) (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall {a}. (Floating a, Num (a -> a)) => a ghci> sin -1 <interactive>:7:1: error: • Non type-variable argument in the constraint: Num (a -> a) (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall {a}. (Floating a, Num (a -> a)) => a -> a ghci> (sin -1) <interactive>:10:1: error: • Non type-variable argument in the constraint: Num (a -> a) (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall {a}. (Floating a, Num (a -> a)) => a -> a ghci> sin (-1) -- -0.8414709848078965 Let's see, ghci> (sin (-1)) -- -0.8414709848078965 Ahh, now that's better.