Created
November 30, 2012 10:58
-
-
Save ikedaisuke/4175124 to your computer and use it in GitHub Desktop.
Answers of the homework in the slides
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- http://www.slideshare.net/yashigani/haskell-in/19 | |
module Homework where | |
import Data.List(foldl') | |
myNull :: [a] -> Bool | |
myNull [] = True | |
myNull _ = False | |
-- integer literal have the typing (Num a) => a. | |
-- http://www.haskell.org/onlinereport/basic.html#numeric-literals | |
-- * accumulated | |
-- * folding | |
mySumAccumulated :: Num a => [a] -> a | |
mySumAccumulated = go 0 | |
where go :: Num a => a -> [a] -> a | |
go x [] = x | |
go x (y : ys) = go (x + y) ys | |
mySumFoldl :: Num a => [a] -> a | |
mySumFoldl = foldl (+) 0 | |
-- a strict version of foldl | |
-- see also http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl%27 | |
mySumFoldl' :: Num a => [a] -> a | |
mySumFoldl' = foldl' (+) 0 | |
-- see the complexity of foldl and foldl' | |
-- > mySumFoldl [0..1000000] | |
-- > mySumFoldl' [0..1000000] | |
-- skip to write myProduct as mySum | |
-- recursive | |
myElem :: Eq a => a -> [a] -> Bool | |
myElem _ [] = False | |
myElem x (y : ys) | |
| x == y = True | |
| otherwise = myElem x ys | |
-- folding | |
myElemFoldl :: Eq a => a -> [a] -> Bool | |
myElemFoldl x = foldl (¥q y -> q || (x == y)) False | |
-- skip to use foldl' instead of foldl | |
-- python's slice | |
sliceLight :: Int -> Int -> [a] -> [a] | |
sliceLight x y zs | |
| x <= y = take (y - x) (drop x zs) | |
| otherwise = undefined | |
-- slice :: Maybe Int -> Maybe Int -> [a] -> [a] | |
-- troublesome... | |
-- fibonacci sequence | |
-- naive definition | |
fib :: Int -> Int | |
fib 0 = 0 | |
fib 1 = 1 | |
fib n = fib (n - 1) + fib (n - 2) | |
-- use infinite sequence and zipWith | |
fibBetter :: Int -> Int | |
fibBetter n = fibs !! n | |
where fibs :: [Int] | |
fibs = 0 : 1 : zipWith (+) fibs (tail fibs) | |
-- see other efficient definitions at HaskellWiki | |
-- http://www.haskell.org/haskellwiki/The_Fibonacci_sequence | |
-- naive definition | |
fizzBuzz :: Int -> String | |
fizzBuzz n | |
| n `mod` 15 == 0 = "FizzBuzz" | |
| n `mod` 5 == 0 = "Fizz" | |
| n `mod` 3 == 0 = "Buzz" | |
| otherwise = show n | |
-- http://www.haskell.org/haskellwiki/Haskell_Quiz/FizzBuzz | |
takeUntilFB :: Int -> Int -> [String] | |
takeUntilFB x y = map fizzBuzz [x..y] | |
-- http://www.slideshare.net/yashigani/haskell-in/21 | |
-- /good/ three-digit number | |
-- TD means three-digit(s) here | |
splitSumTDL :: Int -> Int | |
splitSumTDL x = x `div` 10 + x `mod` 10 | |
splitSumTDR :: Int -> Int | |
splitSumTDR x = x `div` 100 + x `mod` 100 | |
isGoodTD :: Int -> Bool | |
isGoodTD x | |
= 100 <= x && x <= 999 | |
&& x `mod` splitSumTDL x == 0 | |
&& x `mod` splitSumTDR x == 0 | |
-- the question in the slides | |
question :: [Int] | |
question = take 4 $ filter isGoodTD [100..999] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment