mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-01-16 06:05:58 +00:00
Merge branch 'haskell-minor'
This commit is contained in:
commit
752279248e
@ -148,12 +148,12 @@ add 1 2 -- 3
|
|||||||
|
|
||||||
-- Guards: an easy way to do branching in functions
|
-- Guards: an easy way to do branching in functions
|
||||||
fib x
|
fib x
|
||||||
| x < 2 = x
|
| x < 2 = 1
|
||||||
| otherwise = fib (x - 1) + fib (x - 2)
|
| otherwise = fib (x - 1) + fib (x - 2)
|
||||||
|
|
||||||
-- Pattern matching is similar. Here we have given three different
|
-- Pattern matching is similar. Here we have given three different
|
||||||
-- definitions for fib. Haskell will automatically call the first
|
-- definitions for fib. Haskell will automatically call the first
|
||||||
-- function that matches the pattern of the value.
|
-- function that matches the pattern of the value.
|
||||||
fib 1 = 1
|
fib 1 = 1
|
||||||
fib 2 = 2
|
fib 2 = 2
|
||||||
fib x = fib (x - 1) + fib (x - 2)
|
fib x = fib (x - 1) + fib (x - 2)
|
||||||
@ -181,7 +181,7 @@ foldl1 (\acc x -> acc + x) [1..5] -- 15
|
|||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
|
|
||||||
-- partial application: if you don't pass in all the arguments to a function,
|
-- partial application: if you don't pass in all the arguments to a function,
|
||||||
-- it gets "partially applied". That means it returns a function that takes the
|
-- it gets "partially applied". That means it returns a function that takes the
|
||||||
-- rest of the arguments.
|
-- rest of the arguments.
|
||||||
|
|
||||||
add a b = a + b
|
add a b = a + b
|
||||||
@ -319,13 +319,13 @@ Nothing -- of type `Maybe a` for any `a`
|
|||||||
-- called. It must return a value of type `IO ()`. For example:
|
-- called. It must return a value of type `IO ()`. For example:
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = putStrLn $ "Hello, sky! " ++ (say Blue)
|
main = putStrLn $ "Hello, sky! " ++ (say Blue)
|
||||||
-- putStrLn has type String -> IO ()
|
-- putStrLn has type String -> IO ()
|
||||||
|
|
||||||
-- It is easiest to do IO if you can implement your program as
|
-- It is easiest to do IO if you can implement your program as
|
||||||
-- a function from String to String. The function
|
-- a function from String to String. The function
|
||||||
-- interact :: (String -> String) -> IO ()
|
-- interact :: (String -> String) -> IO ()
|
||||||
-- inputs some text, runs a function on it, and prints out the
|
-- inputs some text, runs a function on it, and prints out the
|
||||||
-- output.
|
-- output.
|
||||||
|
|
||||||
countLines :: String -> String
|
countLines :: String -> String
|
||||||
@ -339,43 +339,43 @@ main' = interact countLines
|
|||||||
-- the `do` notation to chain actions together. For example:
|
-- the `do` notation to chain actions together. For example:
|
||||||
|
|
||||||
sayHello :: IO ()
|
sayHello :: IO ()
|
||||||
sayHello = do
|
sayHello = do
|
||||||
putStrLn "What is your name?"
|
putStrLn "What is your name?"
|
||||||
name <- getLine -- this gets a line and gives it the name "name"
|
name <- getLine -- this gets a line and gives it the name "name"
|
||||||
putStrLn $ "Hello, " ++ name
|
putStrLn $ "Hello, " ++ name
|
||||||
|
|
||||||
-- Exercise: write your own version of `interact` that only reads
|
-- Exercise: write your own version of `interact` that only reads
|
||||||
-- one line of input.
|
-- one line of input.
|
||||||
|
|
||||||
-- The code in `sayHello` will never be executed, however. The only
|
-- The code in `sayHello` will never be executed, however. The only
|
||||||
-- action that ever gets executed is the value of `main`.
|
-- action that ever gets executed is the value of `main`.
|
||||||
-- To run `sayHello` comment out the above definition of `main`
|
-- To run `sayHello` comment out the above definition of `main`
|
||||||
-- and replace it with:
|
-- and replace it with:
|
||||||
-- main = sayHello
|
-- main = sayHello
|
||||||
|
|
||||||
-- Let's understand better how the function `getLine` we just
|
-- Let's understand better how the function `getLine` we just
|
||||||
-- used works. Its type is:
|
-- used works. Its type is:
|
||||||
-- getLine :: IO String
|
-- getLine :: IO String
|
||||||
-- You can think of a value of type `IO a` as representing a
|
-- You can think of a value of type `IO a` as representing a
|
||||||
-- computer program that will generate a value of type `a`
|
-- computer program that will generate a value of type `a`
|
||||||
-- when executed (in addition to anything else it does). We can
|
-- when executed (in addition to anything else it does). We can
|
||||||
-- store and reuse this value using `<-`. We can also
|
-- store and reuse this value using `<-`. We can also
|
||||||
-- make our own action of type `IO String`:
|
-- make our own action of type `IO String`:
|
||||||
|
|
||||||
action :: IO String
|
action :: IO String
|
||||||
action = do
|
action = do
|
||||||
putStrLn "This is a line. Duh"
|
putStrLn "This is a line. Duh"
|
||||||
input1 <- getLine
|
input1 <- getLine
|
||||||
input2 <- getLine
|
input2 <- getLine
|
||||||
-- The type of the `do` statement is that of its last line.
|
-- The type of the `do` statement is that of its last line.
|
||||||
-- `return` is not a keyword, but merely a function
|
-- `return` is not a keyword, but merely a function
|
||||||
return (input1 ++ "\n" ++ input2) -- return :: String -> IO String
|
return (input1 ++ "\n" ++ input2) -- return :: String -> IO String
|
||||||
|
|
||||||
-- We can use this just like we used `getLine`:
|
-- We can use this just like we used `getLine`:
|
||||||
|
|
||||||
main'' = do
|
main'' = do
|
||||||
putStrLn "I will echo two lines!"
|
putStrLn "I will echo two lines!"
|
||||||
result <- action
|
result <- action
|
||||||
putStrLn result
|
putStrLn result
|
||||||
putStrLn "This was all, folks!"
|
putStrLn "This was all, folks!"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user