Trying to understand haskell's code -
here haskell code compiles:
class category categ method1 :: categ a method2 :: categ b -> categ b c -> categ c but don't understand meaning:
- what's
categ? how can defined: throughdataorclass? maybefunction? what
a,b,c? since not specified asclass category categ b c method1 :: categ a method2 :: categ b -> categ b c -> categ cthis code shouldn't compile, should it?
class category categ this type class declaration,
it declares type class called category. categ variable used refer type implementing category in associated functions. later fill when say
instance category foo .... then wherever categ used in type class methods, substitute foo , define methods.
read out loud "a type categ category if has following methods"
now methods:
method1 :: categ a method2 :: categ b -> categ b c -> categ c declares 2 functions type implementing category must implement. first 1 no argument function returns, type a, type categ a. method2 takes in 2 pieces of data, categ b , categ b c, , returns categ c.
here a b c type variables, placeholders filled arbitrary concrete types when function called. if you're familiar c++ or java,
template<typename a> categ<a, a> method1(); is pretty close have in haskell. in haskell, use type variables without declaring them. function local, , equivalent saying like, for type a, following code works.
this pretty fundamental haskell, i'd recommend reading learn haskell. it's free , cover aspects of code posted.
Comments
Post a Comment