Skip to main content

Home/ Haskell/ Group items tagged comparation

Rss Feed Group items tagged

Javier Neira

jaspervdj - Maybe, null, and boilerplate code - 0 views

  • A first goal they serve is as "unassigned variables". I will not cover that here, instead I will focus on another use: abnormal return values.
  • So, null is some sort of "Not found" error here. But wait - they could have used an Exception here!
  • We see a similar behavior here: when the object is found, we get a Just a, and when it is not found, we get Nothing - comparable to a null pointer.
  • ...5 more annotations...
  • So, while we can get a null back from a function, we should never give it to a function.
  • Now, if you have written a lot of Java code, you know that checking for null is a vital part of the job, and fragments like the one above are pretty common.
  • In Haskell, however, Maybe is also a monad - and monads can be used to prevent common patterns in code - in other words, they assist you in the D.R.Y.-principle2. We can therefore write the above snippet again using do-notation3.
  • getPathMonth :: Map String String -> Maybe MonthgetPathMonth m = do p <- lookup "path" m d <- parseDate p return $ getMonth d
  • he whole "check-for-null-and-short-circuit" behavior is defined by the Maybe monad -
  •  
    getPathMonth :: Map String String -> Maybe Month getPathMonth m = case M.lookup "path" m of Nothing -> Nothing (Just p) -> case parseDate p of Nothing -> Nothing (Just d) -> Just (getMonth d) getPathMonth :: Map String String -> Maybe Month getPathMonth m = do p <- parseDate p return $ getMonth d
Javier Neira

The best programming languages - 0 views

  • Haskell is not a programmable programming language (unless you look at extensions like template Haskell) but there are enough abstractions in Haskell to easily build domain specific languages with it.
  • But, the idea of time, the idea of sequencing can be built upon a purely functional system and we are led to monads
  • Monad are the recognition that the idea of sequencing operations can be formalized in a purely functional language.
  • ...2 more annotations...
  • The monad is an abstraction allowing to create new control structures and, in associations with the powerful type system of Haskell which makes possible elegant overloading of operators, it is possible to easily build new abstractions that allow to customize the programming language.
  • Unfortunately, Lisp, Smalltalk and Haskell are too different from most programming languages currently used in industry and taught in schools. They really look like alien technology to most developpers.
1 - 7 of 7
Showing 20 items per page