Skip to main content

Home/ Haskell/ Group items tagged function

Rss Feed Group items tagged

Javier Neira

Smart constructors - HaskellWiki - 0 views

  • The most interesting are probably the static checks that can be done with Type arithmetic, that enforce the number of bands at compile time, rather than runtime, by lifting the band count into the type level.
  • typecheck perform the check statically, using phantom types and Peano numbers.
  • So encode a type-level version of the bounds check. Only resistors with bands >= 4 and <= 8 are valid:
  • ...1 more annotation...
  • Further checks can be obtained by separating the metal and ceramic values on the type level, so no function that takes a metal resistor can be accidentally passed a ceramic one. A newtype is useful for this:
J.A. Alonso

A tour of the Haskell monad functions - 1 views

  • ap module: Control.Monad type: ap :: (Monad m) => m (a -> b) -> m a -> m b
J.A. Alonso

Functional Programming in Haskell (Part 2 : Abstract dataypes and ``infinite'' structures) - 0 views

  •  
    Introducción a la programación en Haskell por Madhavan Mukund
Javier Neira

Monads in 15 minutes: Backtracking and Maybe - 0 views

  • type Choice a = [a] choose :: [a] -> Choice a choose xs = xs
  • Because Haskell doesn’t compute answers until we ask for them, we get the actual backtracking for free!
  • The missing function is almost too trivial to mention: Given a single value of type a, we need a convenient way to construct a value of type Choice a:
  • ...5 more annotations...
  • More math trivia: return is also known as unit and η. That’s a lot of names for a very simple idea.)
  • makePairs = choose [1,2,3] >>= (\x -> choose [4,5,6] >>= (\y -> return (x,y)))
  • makePairs' = do x <- choose [1,2,3] y <- choose [4,5,6] return (x,y)
  • Every monad has three pieces: return, map and join.
  • Backtracking: The lazy way to code
Javier Neira

The Haskell 98 Library Report: Arrays - 0 views

  • 16.2  Incremental Array Updates The operator (//) takes an array and a list of pairs and returns an array identical to the left argument except that it has been updated by the associations in the right argument. (As with the array function, the indices in the association list must be unique for the updated elements to be defined.) For example, if m is a 1-origin, n by n matrix, then m//[((i,i), 0) | i <- [1..n]] is the same matrix, except with the diagonal zeroed.
  • -- A rectangular subarray subArray :: (Ix a) => (a,a) -> Array a b -> Array a b subArray bnds = ixmap bnds (\i->i) -- A row of a matrix row :: (Ix a, Ix b) => a -> Array (a,b) c -> Array b c row i x = ixmap (l',u') (\j->(i,j)) x where ((_,l'),(_,u')) = bounds x -- Diagonal of a matrix (assumed to be square) diag :: (Ix a) => Array (a,a) b -> Array a b diag x = ixmap (l,u) (\i->(i,i)) x        where           ((l,_),(u,_)) = bounds x -- Projection of first components of an array of pairs firstArray :: (Ix a) => Array a (b,c) -> Array a b firstArray = fmap (\(x,y)->x)
« First ‹ Previous 41 - 48 of 48
Showing 20 items per page