Skip to content

Instantly share code, notes, and snippets.

@vvv
Forked from mssawant/misc.hs
Last active August 24, 2017 16:50
Show Gist options
  • Save vvv/2b3fa9fcf51e47bf69aa482cf2b4ffcb to your computer and use it in GitHub Desktop.
Save vvv/2b3fa9fcf51e47bf69aa482cf2b4ffcb to your computer and use it in GitHub Desktop.
Haskell exercise
{-- XXX Your definition of `checkChar` is okay, but the abstraction itself
-- is very odd. It is fine as an exercise of using "do" notation and
-- `return`. In real code though explicit statements are both short and
-- expressive (see lines 21-22).
checkChar :: IO Bool
checkChar = do
c <- getChar
return (c == 'y')
--}
-- XXX There is a name for this operation -- "cons";
-- see [https://en.wikipedia.org/wiki/Cons].
-- Haskell has cons operator -- `(:)`.
-- addList = (:)
addList :: a -> [a] -> [a] -- XXX Note that the type is not limited to `Int`.
addList y xs = y:xs
main :: IO () -- XXX Surround `::` with spaces from both sides.
main = do
putStr "Enter char: "
c <- getChar
putStrLn $ if c == 'y' then "TRUE" else "FALSE" -- XXX `if` expression gets evaluated and `putStrLn` uses its result.
-- XXX `let` statement is unnecessary here. `print [1, 2]` would do.
let l = [1, 2]
print l -- XXX `$` is not needed here.
let m = addList 3 l
print m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment