Calling function in haskell
I'm pretty new in Haskell programming. I want to call some functions and save the result in a variable but I don't know how. I read couple of chapters about haskell function in two different book but still don't understand how to do it.
import Data.Map (Map)
import qualified Data.Map as M hiding (Map)
newtype GenEnv elt = Env (Map Id elt)
newEnv :: GenEnv elt -- initialise
newEnv = Env M.empty
newtype GenEnv elt = Env (Map Id elt)
newEnv :: GenEnv elt -- initialise
newEnv = Env M.empty
getEnv :: GenEnv elt -> Id -> Maybe elt -- G(x) (key function)
getEnv (Env env) var = M.lookup var env
union :: GenEnv elt -> (Id,elt) -> GenEnv elt -- G[x:v]
union (Env env) (key,elt) = Env (M.insert key elt env)
-- foldr is faster than addToFM_list!
unionL :: GenEnv elt -> [(Id,elt)] -> GenEnv elt -- list union
unionL (Env env) pairs = Env $ foldr (\(k,e) g -> M.insert k e g) env pairs
What I'm asking here is NOT for somebody to do my wo开发者_如何学编程rk, asking how to call the functions because I don't understand their signature.
As others have mentioned, "variable" is perhaps not the right term. And in the same vein, "calling" a function is perhaps not the right term either. It is helpful, in my opinion, to think about this in terms of mathematical functions:
f(x) = x^2
given the above function, you don't "call" it with a value so much as give a name to the result of evaluating that function at a particular argument:
y = f(2)
It's the same in Haskell. Somewhere in your code you need to use the result of evaluating a function at a particular value. To do that, you can just use the application of that function to that value wherever you need it, or you can bind it to a name in a let
binding or a where
clause.
So, to provide a simple example in Haskell, you can do something like this:
f :: Int -> Int
f x = x^2
y :: Int
y = f 2
g :: Int -> Int
g x = let y = f 3
in y + 1
h :: Int -> Int
h x = y + 1
where y = f 3
Here I have declared a function called f
which takes a single Int
value and returns a new Int
value, the square of the argument. Then I have declared an Int
value named y
to be the result of applying f
to 2
. The value y
is not a variable, but rather a binding. It will always be 4
.
Then I have declared two other functions, g
and h
which are equivalent, showing local bindings of the results of applying f
.
In your example, the types are perhaps complicating things a little bit. Env
is a constructor used to construct a value of the Genenv
type. So, to create a value that is a Genenv
type, you apply Env
to an appropriate argument. This is what newEnv
is doing.
Hopefully that's enough to get you started.
Since I pretty much suck at explaining these things, I'd recomment reading this chapter
http://book.realworldhaskell.org/read/types-and-functions.html
should cover everything you need to know to be able to call those functions.
In general in Haskell we apply a function to some arguments and bind the results to some value.
Haskell doesn't have variables, first of all. Generally we talk of binding an expression to an identifier, creating a thunk.
It has the form of
let val = unionEl g l
in --expression using val
In Haskell, function application is done through juxtaposition, but you can use parentheses first to get the feel for it, just don't use commas to separate parameters like you may be used to in other languages.
For instance, assuming Id is just a type synonym for Int, then the following would be an example GenEnv.
example :: GenEnv Char
example = union newEnv (1, 'a')
which is the same as
example :: GenEnv Char
example = union(newEnv (1, 'a'))
Say you have a list of the alphabet, associated with an integer value, called alphabetKeys
, you can use the handy little tool you defined to turn that into a GenEnv pretty easily, just by
example :: GenEnv Char
example = union newEnv alphabetKeys
again,
example :: GenEnv Char
example = union(newEnv alphabetKeys)
would work just as well.
精彩评论