-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modified PR #4
Open
Abhiroop
wants to merge
6
commits into
ashishnegi:master
Choose a base branch
from
Abhiroop:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Modified PR #4
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cd60fc6
Contain solution to first exercise
Abhiroop 4c957bf
Merge remote-tracking branch 'upstream/master'
Abhiroop 860d214
Move solution to different file
Abhiroop 3a77369
Restore old version of Main.hs
Abhiroop fe75582
Solve lesson 2 and 3 exercises
Abhiroop 582227f
Add local maxima solution
Abhiroop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module Main where | ||
|
||
-- How you are supposed to do this exercise ? | ||
-- 1) Recursion : This is a powerful thing. | ||
-- In haskell, you would see recursion at a lot of places. | ||
-- and it is easier to do as well.. | ||
-- 2) writing sub-functions : | ||
|
||
-- See this example ; | ||
-- I have to write a function that give a number N | ||
-- gives me a list that has N "Hello" strings. | ||
-- Types says it all :) | ||
sayNHello :: Int -> [String] | ||
-- I will create a sub-function.. we use "where" for that.. | ||
sayNHello n = sayNHello' 0 n [] | ||
where | ||
-- this is how you write sub-function | ||
-- we do not need to write types here as compiler derives it for us.. | ||
-- but just for sanity | ||
sayNHello' :: Int -> Int -> [String] -> [String] | ||
sayNHello' i n resultSoFar = | ||
if i < n | ||
-- keep appending to the list if we are not done yet.. | ||
then sayNHello' (i + 1) n ("Hello" : resultSoFar) | ||
else resultSoFar -- otherwise we are done.. | ||
|
||
toDigits :: Integer -> [Integer] | ||
toDigits 0 = [] | ||
toDigits n = toDigits (n `div` 10) ++ [n `mod` 10] | ||
|
||
toDigitsRev :: Integer -> [Integer] | ||
toDigitsRev 0 = [] | ||
toDigitsRev n = n `mod` 10 : toDigitsRev (n `div` 10) | ||
|
||
-- You need to map over every element of the list.. | ||
-- But you can use recursion for it as well.. | ||
-- Otherwise you can write to the top of file after `module ..` | ||
-- import qualified Data.List as DL | ||
-- and use DL.map.. | ||
-- but remember it is all about writing Recursion at this point.. | ||
doubleEveryOther :: [Integer] -> [Integer] | ||
doubleEveryOther (x:y:xs) = x:(2 * y):doubleEveryOther xs | ||
doubleEveryOther x = x | ||
|
||
sumDigits :: [Integer] -> Integer | ||
sumDigits [x] = x | ||
sumDigits (x:xs) = x + sumDigits xs | ||
|
||
validate :: Integer -> Bool | ||
validate n = undefined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please see cs 194 homework1.pdf for what needs to be done in the function.. |
||
|
||
main :: IO () | ||
main = do | ||
putStrLn "Hello world" | ||
-- show takes a type and convert to string | ||
putStrLn $ show 1 | ||
putStrLn $ show [1 , 2 , 3] | ||
putStrLn $ show True | ||
-- Exercise : | ||
-- call all above functions here.. | ||
putStrLn $ show $ sayNHello 5 | ||
putStrLn $ show $ toDigits 12345 | ||
putStrLn $ show $ toDigitsRev 12345 | ||
putStrLn $ show $ doubleEveryOther [1 , 2 , 3 , 4 , 5] | ||
putStrLn $ show $ sumDigits [1 , 2 , 3] | ||
|
||
|
||
-- syntax : | ||
ifSyntax num = if num > 0 | ||
then num - 1 | ||
else num + 1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens with
[]
list as argument ?