Created
December 18, 2012 17:37
-
-
Save feltnerm/4330113 to your computer and use it in GitHub Desktop.
Some Haskell problems that I did for fun
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
{-- | |
- | |
- @author: Mark Feltner | |
- | |
- **Haskell** | |
- | |
- Haskell is a nice language. I enjoyed it better than F# for functional | |
- programming. | |
- From my limited perspective, it seems that Haskell would operate best | |
- in a highly theoretical or mathematical environment; not the environment | |
- I would consider myself most accustomed to. | |
- Its ability to process lazily and its functional origins mean it has | |
- great capabilities for processing lists. Again, from my limited introduction, | |
- it is hard for me to see the practical use of this apart from solving | |
- mathematical or academic types of problems. | |
- I enjoyed learning Haskell as it was somewhat like reading | |
- your favorite book for the first time again. I got to re-learn the "basics" | |
- of programming: iterating through lists, checking conditions, operating | |
- based on type, and basic mathematics. | |
- If I could, I would program Haskell on a regular basis. I feel like | |
- I can almost see the way to knowledge of functional programming, and I | |
- think it would be a cool domain to work and learn in. Haskell is not | |
- a language (at least for me) that makes you want to rip your hair out. | |
- Its snytax is clean and readable, and the concepts behind it, once learned, | |
- make sense. Also, working with people who know Haskell would mean that | |
- I'm working with people who enjoying coding not just for the money, but | |
- because they are actually interested in coding and want to learn the different | |
- ways ands and methods they can do things and not just the languages that | |
- are 'force-taught' in academia. | |
- As for now, I think I'll be mostly applying ideas and methodologies picked | |
- up from learning Haskell in imperative languges (especially ones like Ruby, | |
- Python, and Javascript where one can implement functional methods and | |
- styles as they please). | |
-} | |
{- --------------------------------------------------------------------------- | |
- | |
- PROJECT EULER | |
- | |
- http://projecteuler.net/ | |
- | |
- --------------------------------------------------------------------------- | |
-} | |
{- Problem 1: Find the sum of all the multiples of 3 or 5 below 1000. -} | |
multsOf3or5 = [x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0 ] | |
problem1 = sum multsOf3or5 | |
-- output: 233168 | |
{- | |
- Problem 2: Find the sum of the even-value terms of the Fibonacci sequence | |
- whose values do not exceed four million | |
- -} | |
fibs = 1 : 2 : zipWith (+) fibs (tail fibs) | |
problem2 = sum $ filter even $ takeWhile (<= 4000000) fibs | |
-- output: 461372 | |
{- --------------------------------------------------------------------------- | |
- | |
- 99 Haskell Problems | |
- | |
- http://www.haskell.org/haskellwiki/H-99:_Ninety-Nine_Haskell_Problems | |
- | |
- --------------------------------------------------------------------------- | |
-} | |
{- Ch 1 - 10 :: Lists -} | |
-- 1: Find the last element in any list. | |
lastEl [] = error "List is empty" | |
lastEl [x] = x | |
lastEl (hd:tail) = lastEl(tail) | |
-- output: lastEl [1,2,3] | |
-- 3 | |
-- note: could also just use bulitin: last | |
-- 2: Find last but one element in any list. | |
lastButOne [] = error "List is empty" | |
lastButOne [hd, tail] = hd | |
lastButOne (hd:tail) = lastButOne(tail) | |
-- output: lastButOne [1,2,3] | |
-- 2 | |
-- 3: Find the k'th element in a list. Index starts at 1 | |
elementAt lst k = lst!!(k-1) | |
-- output: elementAt [1,2,3] 2 | |
-- 2 | |
-- 4: Find the number of elements in a list | |
numElements [] = 0 | |
numElements [x] = 1 | |
numElements (hd:tl) = 1 + numElements(tl) | |
-- output: numElements [1,2,3] | |
-- 3 | |
-- note: could also just use builtin: length | |
-- 5: Reverse a list | |
rev [] = [] | |
rev (hd:tail) = rev(tail)++[hd] | |
-- ouput: rev [1,2,3] | |
-- [3,2,1] | |
-- note: could also just use builtin: reverse | |
-- 6: Find out whether a list is a palindrome. A palindrome can be read forward or backward; e.g. (x a m a x).) | |
palin [] = False | |
palin (list) = | |
if (list == rev list) | |
then True | |
else False | |
-- output: palin ['x','y','y','x'] | |
-- True | |
-- 7. Transform a list, possibly holding lists as elements into a flat list by replacing each list with its elements (recursively). | |
data NestedList a = Elem a | List [NestedList a] | |
flatten :: NestedList a -> [a] | |
flatten (Elem a) = [a] | |
flatten (List (a:as)) = flatten a ++ flatten( List as) | |
flatten (List []) = [] | |
-- output: flatten (List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 5]]) | |
-- [1,2,3,4,5]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing ❤️