Constructors versus functions
Constructors create new data types from old data. Let's expand our own list:
data List a = Empty | Cons a (List a)
myList = Const 1 (Cons 2 Empty) -- 1:2:[] =[1,2]
uncons x' (Cons x xs) = if x == x' then xs else Cons x xs
Here Cons :: a -> List ais a special function that takes an element and a list and makes a longer list.
It is important that there is a difference between design functions and regular functions, so the compiler knows which one is valid in the template:
headM (Cons x xs) = Just x
headM Empty = Nothing
This makes sense, but it is not:
previousHead (uncons x xs) = Just x
previousHead xs = Nothing
because how can a computer know which item you deleted or did you delete it?
Infix Constructors and Functions
, , ,
data [a] = [] | a:[a]
, 1:2:[].
Infix , x:xs , infix ( infix, :, , .
- , , , . : , - , .
: =
:= ,
data Assignment a = Variable := Expression a
:=, , : , , - , (, ) :
(.:=) :: Maybe a -> a -> Maybe a
Nothing .:= x = Just x
Just y .:= x = Just x