haskell - Application ($) operator acting unexpectedly -
I am writing a function that generates the collage series based on an initial number, but I have an unexpected problem < / P>
Here is the code:
- Basic, collatzA :: integer - & gt; [Integer] colatz 1 = [1] colatz anne | Even n = n: colatz A (n`div` 2). Strange N = n: Colatz A (N * 3 + 1) - which I'm trying to do, will not compile it, will accumulate dirty errors collatzB :: Integer - & gt; [Integer] collatzB 1 = [1] collatzB n | Even n = n: collatzB $ n` div` 2. Strange n = n: collatzB $ n * 3 + 1 - Attempts to solve, works, but adds brackets again; I have collatzC :: Integer - & gt; Tried to get rid of [Integer] collatzC 1 = [1] collatzC n | Even n = n: (collatzC $ n` div` 2). Weird n = n: (collatzC $ n * 3 + 1) Why is it that collatzA and collatzC work , But is not collatzB
This problem is operators preference or stability .
For example (from which I have been highly recommended) (+) with Stability 6 and (*) Left- as left - stability with companionship 7. It means expression
8 + 7 + 6 * 5 * 4 is parsed
(<+++> (8 + 7) + ((6 * 5) * 4) :)
is correct-associative and has a fixity of 5, while app operator ($) correct-cum-related There is a doubt and it determines 0. Since ($) is less than Dexterity (:) , collatzB is a recursive call (:) n = (n: collatzB) $ (n` div` 2) has precision information for the prelude function, and you can also see for more information. You can.
Comments
Post a Comment