reset password
Author Message
rabbott
Posts: 1649
Posted 04:49 Sep 13, 2017 |

Programming is about thinking clearly and then expressing your thoughts in a programming language. The higher the level of the language the better suited it is to letting you express thoughts directly. Haskell is a pretty high level language. But it is still a programming language. Writing a Haskell program is almost never the same as expressing the idea behind the program. 

One of the primary reasons to purse an education is to become more skilled in thinking clearly -- and expressing your thoughts clearly. Learning functional programming can certainly help. But functional programming isn't all there is to thinking clearly. I want you all to focus on thinking clearly and learning how to express yourself clearly.

As undergraduates you should have learned about the difference between declarative and procedure programming. Declarative programming, to the extent it is possible at all, is expressing directly what you want to accomplish. Procedural programming is saying, step by step, how to accomplish that objective. 

For example, suppose I want the next prime larger than a given integer. The following program will do it.

{- 
 Returns the smallest prime greater than the argument.
 -}
nextPrime :: Integral a => a -> a
nextPrime n = head [p | p <- [n+1 ..], not (isComposite p)]

{- 
 Returns true if there is an integer greater than 1 that 
 divides evenly into the argument; false otherwise.
 -}
isComposite :: Integral a => a -> Bool
isComposite n = not (null [d | d <- [2 .. floor (sqrt (fromIntegral n))], n `mod` d == 0])

 

Notice the difference between the functions and the descriptions. 

 

  • The function nextPrime shows how to find the next larger prime. The statement "Returns the smallest prime greater than the argument" says what the function returns.

 

  • The function isComposite shows how to find a list of divisors for a number. The statement "Returns true if there is an integer greater than 1 that divides evenly into the argument; false otherwise." says what the function returns.

 

Your comments before each of your functions should say declaratively what the function returns, not how it goes about producing that result.

When you explain a function f that uses some other function g, you should describe the function g declaratively. Say what g returns and how that result is useful in the context where it is called within f.

You will sooner or later forget how to write Haskell code (probably sooner). I hope that you won't forget how to think and express yourselves clearly.

Last edited by rabbott at 04:55 Sep 13, 2017.