Skip to content
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
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions app/Main_abhiroop.hs
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
Copy link
Owner

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 ?

doubleEveryOther x = x

sumDigits :: [Integer] -> Integer
sumDigits [x] = x
sumDigits (x:xs) = x + sumDigits xs

validate :: Integer -> Bool
validate n = undefined
Copy link
Owner

Choose a reason for hiding this comment

The 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..
this fn is fun.


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