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:

  1. what's categ? how can defined: through data or class? maybe function?
  2. what a , b , c? since not specified as

    class category categ b c     method1 :: categ a     method2 :: categ b -> categ b c -> categ c 

    this 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

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -