(\a -> a) = id (\a b -> a) = const (\a b -> b) = const id (\a b c -> a) = const . const (\a b c -> b) = const const (\a b c -> c) = const (const id) (\a b c d -> a) = const . const . const (\a b c d -> b) = const (c . c) (\a b c d -> c) = c (c c) (\a b c d -> d) = c (c (c id))
-- this example shows how to read an Int from the command line, safely. -- semi-helpful info here: http://www.reddit.com/r/haskell/comments/1vras3/haskell_io_how_to_read_numbers/ main = do -- prompt the user and get their input putStrLn "Enter an Int: " line <- getLine -- convert `line` (whose type is `String`) to a value of type `Maybe Int` let maybeInt = readMaybe line :: Maybe Int case maybeInt of Just n -> putStrLn (show (factorial n)) >> main -- `>> main` repeats the loop (asking for user input) Nothing -> putStrLn "Please try again." >> main -- converts an input `String` (i think) into a `Maybe a`. -- in this example i use it to get a "Maybe Int" from the user's input. readMaybe :: Read a => String -> Maybe a readMaybe s = case reads s of [(val, "")] -> Just val _ -> Nothing -- factorial function factorial :: Int -> Int factorial n = if n < 2 then 1 else n * factorial (n-1)
module InsSort where ins :: Ord a => a -> [a] -> [a] ins e [] = [e] ins e (x:xs) | e < x = e : x : xs | otherwise = x : ins e xs insSort :: Ord a => [a] -> [a] insSort [] = [] insSort (x:xs) = ins x (insSort xs)