123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- module Haskell1 where
- ones = 1 : ones
- fib = 1 : 1 : [a+b | (a,b) <- zip fib (tail fib)]
- mytake 0 _ = []
- mytake _ [] = []
- mytake n (x:xs) = x : mytake (n-1) xs
- myfoldl f z [] = z
- myfoldl f z (x:xs) = myfoldl f (f x z) xs
- instance Show (a -> b) where
- show f = "<< a function>>"
- -- define my-lenght
- mylenght :: [a] -> Int
- mylenght [] = 0
- mylenght (x:xs) = 1 + mylenght xs
- myrange :: Int -> Int -> [Int]
- myrange a b = if a > b
- then error "Low > High"
- else if a < b
- then a : myrange (a +1) b
- else [a]
- data TrafficLight = Red | Yellow | Green
- data Answer = Yes | No
- data Tree a = EmptyTree | Node a (Tree a) (Tree a)
- data Person = Person { firstname :: String
- , lastname :: String
- , age :: Int
- , height :: Float
- , phonenumber :: String
- }
- mymap :: (a -> b) -> [a] -> [b]
- mymap _ [] = []
- mymap f (x:xs) = f x : mymap f xs
- -- Re-write the myrange with boolean guards
- myrange2 :: Int -> Int -> [Int]
- myrange2 a b
- | a > b = error "low > high"
- | a == b = [b]
- | a < b = a : myrange2 (a + 1) b
- -- define myrange with only one input parameter
- myrange3 :: Int -> [Int]
- myrange3 a = a : myrange3 (a + 1)
- --describe the type of a list
- describelist :: [a] -> String
- describelist xs = "The list is " ++ case xs of
- [] -> "Empty List"
- [x] -> "One element / Singleton"
- xs -> "Multiple Elements"
- --definition of confitional take from list
- myTakeWhile :: (a -> Bool) -> [a] -> [a]
- myTakeWhile _ [] = []
- myTakeWhile f (x:xs)
- | f x = x : myTakeWhile f xs
- | otherwise = []
- myFilter :: (a -> Bool) -> [a] -> [a]
- myFilter _ [] = []
- myFilter c (x:xs)
- | c x = x : myFilter c xs
- | otherwise = myFilter c xs
- --define myReverse
- myReverse :: [a] -> [a]
- myReverse = foldl (\acc x -> x:acc) []
- --define mySum, a function that sums all the elements of a list
- mySum :: [Int] -> Int
- mySum [] = 0
- mySum (x:xs) = x + mySum xs
- mySum2 :: (Num a)=>[a] -> a
- mySum2 = foldl (\acc x -> acc + x) 0
- --define myFilter2 using foldr
- myFilter2 :: (a -> Bool) -> [a] -> [a]
- myFilter2 f = foldr (\x acc -> if f x then x:acc else acc) []
|