About
haskell
infix a+b
prefix (+) a b
unary minus -3
Precedence parsing error
2 + -3 -> cannot mix +' [infixl 6] and prefix
-' [infixl 6] in the same infix expression
ghci> 2*-3
<interactive>:13:2: error:
* Variable not in scope: (*-) :: t0 -> t1 -> t
* Perhaps you meant one of these:
`*>' (imported from Prelude), `**' (imported from Prelude),
`*' (imported from Prelude)
Haskell reads - as a single operator. Could be used if defined.
Operator Description Associativity
. Function composition Right
^, ^^, ** Exponentiation Right, /, div, mod, rem Multiplication and division Left
+, - Addition and subtraction Left
:, ++ List construction and concatenation Right
==, /=, <, <=, >, >= Comparison None
&& Logical conjunction (AND) Right
`
Let e = exp 1 e = 2.718281828459045
pi
[1,2,] <interactive>:28:6: error: parse error on input
]‘
["foo".."quux"]
<interactive>:35:1: error:
* No instance for (Enum String)
arising from the arithmetic sequence `"foo" .. "quux"'
* In the expression: ["foo" .. "quux"]
In an equation for `it': it = ["foo" .. "quux"]
Concatenation operator ++
Cons operator : (construct) add in front of the list
Haskell type names must start with an uppercase letter, and variable names must start with a lowercase letter
:set +t 3 ‘a’ True for Type
The word “it’” is the name of a special variable, in which ghci (not Haskell ! ) stores the result of the last expression we evaluated.
ghci> :m +Data.Ratio (:m is the short form for :module command)
ghci> 11 % 29
11 % 29
it :: Integral a => Ratio a
:unset +t
:type 'a'
compare a b
take - Given a number n and a list, take returns the first n elements of the list
drop -returns all but the first n elements of the list
ghci> take 2 [1,2,3,4,5]
[1,2]
ghci> drop 3 [1,2,3,4,5]
[4,5]
For tuples, the fst and snd functions return the first and second element of a pair, respectively:
ghci> fst (1,'a')
1
ghci> snd (1,'a')
'a'
ghci> :type 'a' Automatic type inference 'a' :: Char ghci> 'a' :: Char Explicit type declaration 'a' ghci> [1,2,3] :: Int type signature but wrong
<interactive>:30:1: error :type (True, "hello") (True, "hello") :: (Bool, String) ghci> head (drop 4 "azerty") 't‘ ghci> head drop 4 "azerty"
<interactive>:57:6: error:
• The pre-defined function lines splits a string on line boundaries ghci> lines "the quick\nbrown fox\njumps" ["the quick","brown fox","jumps"]
Impure fns Example: ghci> :type readFile readFile :: FilePath -> IO String name <- getLine
quicksort :: (Ord a) => [a] -> [a] quicksort [] = [] quicksort (x:xs) = let smallerSorted = quicksort [a | a <- xs, a <= x] biggerSorted = quicksort [a | a <- xs, a > x] in smallerSorted ++ [x] ++ biggerSorted ghci> quicksort [10,2,5,3,1,6,7,4,2,3,4,8,9] [1,2,2,3,3,4,4,5,6,7,8,9,10] ghci> quicksort "the quick brown fox jumps over the lazy dog" " abcdeeefghhijklmnoooopqrrsttuuvwxyz"
import Data.List (group, sort)
mostFrequent :: Ord a => [a] -> a mostFrequent xs = snd (last (sort freqs)) where freqs = map (\xs -> (length xs, head xs)) (group (sort xs))
main :: IO () main = do let myList = [1, 2, 3, 2, 3, 2, 1, 4, 2, 3, 3] putStrLn ("List: " ++ show myList) let freq = mostFrequent myList putStrLn ("Most frequent element: " ++ show freq)
maximum' :: (Ord a) => [a] -> a maximum' = foldr1 (\x acc -> if x > acc then x else acc) reverse' :: [a] -> [a] reverse' = foldl (\acc x -> x : acc) [] product' :: (Num a) => [a] -> a product' = foldr1 (*) filter' :: (a -> Bool) -> [a] -> [a] filter' p = foldr (\x acc -> if p x then x : acc else acc) [] head' :: [a] -> a head' = foldr1 (\x _ -> x) last' :: [a] -> a last' = foldl1 (_ x -> x)