Thursday, October 13, 2005

Haskell - lecture 7

-- isLower, isUpper etc. are not recognized in WinHugs if the prompt says "Main>" (but it is recognized on startup when the prompt is "Prelude>")
kindOfChar :: Char -> String
kindOfChar c =
if (isLower c)
then "lower"
else if isUpper c
then "upper"
else if isDigit c
then "digit"
else "other"
-- Same function implemented with guards:
kindOfChar2 :: Char -> String
kindOfChar2 c -- Notice that there is no "=" before the guards
| isLower c = "lower"
| isUpper c = "upper"
| isDigit c = "digit"
| otherwise = "other"

-- Acts like the prelude (built-in) function 'take'
myTake :: Int -> [a] -> [a]
myTake 0 list = []
myTake m [] = []
myTake m x:xs =
x : (myTake (m-1) xs)
-- Implementation using "case" expression
-- Internally, this is how Haskell resolves multiple function declarations?
myTake2 :: Int -> [a] -> [a]
myTake2 n list = case (n,list) of
(0, list) -> []
(n, []) -> []
(n, x:xs) -> c : myTake2 (n-1)

-- In Haskell
-- Everything exists at the top level (mutually recursive (?))
-- Basic libraries automatically exist in prelude
-- Type synonym
-- Type classes (Ord, Num, Fractional, ...)
-- Read and Show
-- Pattern findings

0 Comments:

Post a Comment

<< Home