Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial homework

  • Loading branch information...
commit 8f0f7199feb2b53c02822315889268e340f1734f 0 parents
@aasmith authored
Showing with 81 additions and 0 deletions.
  1. +23 −0 ch3.hs
  2. +6 −0 ch4.hs
  3. +52 −0 foo.hs
23 ch3.hs
@@ -0,0 +1,23 @@
+second :: [a] -> a
+second xs = head (tail xs)
+
+swap :: (a,b) -> (b,a)
+swap (x,y) = (y,x)
+
+pair :: x -> y -> (x,y)
+pair x y = (x,y)
+
+double :: (Num x) => x -> x
+double x = x * 2
+
+palindrome :: (Eq a) => [a] -> Bool
+palindrome xs = reverse xs == xs
+
+twice :: (a -> a) -> (a -> a)
+twice f x = f (f x)
+
+-- twice takes:
+-- (a -> a) a function that takes something and returns something
+-- such as double(x)
+-- -> and twice itself returns
+-- (a -> a) a function as above
6 ch4.hs
@@ -0,0 +1,6 @@
+abs :: Int -> Int
+abs x = if x < 0 then -x else x
+
+signum :: Int -> Int
+signum n = if n < 0 then -1 else
+ if n == 0 then 0 else 1
52 foo.hs
@@ -0,0 +1,52 @@
+prod (x:xs) = x * prod xs
+prod x = 1
+
+fac n = prod [1..n]
+
+-- workslike sum
+summ (x:xs) = x + summ xs
+summ x = 0
+
+-- workslike length
+len (x:xs) = 1 + len xs
+len [] = 0
+
+flatten (xs:xss) = xs ++ flatten xss
+flatten xs = []
+
+-- workslike last, old thinking
+oldend (xs) = xs !! (length xs - 1)
+
+-- workslike last
+end (x:[]) = x -- when there is one element, and an
+ -- empty list remaining (i.e. end of list), return x
+end (_:xs) = end xs -- when there is one element, and something
+ -- else that is not an empty list, recurse passing in
+ -- the not empty list
+end [] = undefined -- fallthrough case when end is called on empty list.
+
+-- workslike init [1,2,3] # => [1,2]
+noend (x:[]) = []
+noend (x:xs) = [x] ++ noend(xs)
+noend [] = []
+
+-- workslike fst (a,b) # => a
+first (a,_) = a
+
+-- workslike reverse
+backwards :: [a] -> [a]
+backwards (x:xs) = backwards xs ++ [x]
+backwards x = []
+
+avg :: (Fractional a) => [a] -> a
+avg xs = summ xs / fromIntegral (len xs)
+-- :
+-- prod 5
+-- fac 4
+
+--
+divide a b = a / b
+-- :
+-- 3 `divide` 2
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.