mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 23:33:00 +03:00
Use Csharp syntax highlighting for fsharp
This commit is contained in:
parent
745760ff18
commit
aac147f3e9
@ -15,7 +15,7 @@ The syntax of F# is similar to Python:
|
|||||||
|
|
||||||
If you want to try out the code below, you can go to [tryfsharp.org](http://www.tryfsharp.org/Create) and paste it into an interactive REPL.
|
If you want to try out the code below, you can go to [tryfsharp.org](http://www.tryfsharp.org/Create) and paste it into an interactive REPL.
|
||||||
|
|
||||||
```fsharp
|
```csharp
|
||||||
|
|
||||||
// single line comments use a double slash
|
// single line comments use a double slash
|
||||||
(* multi line comments use (* . . . *) pair
|
(* multi line comments use (* . . . *) pair
|
||||||
|
@ -89,7 +89,8 @@ last [1..5] -- 5
|
|||||||
-- with a conditional
|
-- with a conditional
|
||||||
[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10]
|
[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10]
|
||||||
|
|
||||||
-- Every element in a tuple can be a different type, but a tuple has a fixed length.
|
-- Every element in a tuple can be a different type, but a tuple has a
|
||||||
|
-- fixed length.
|
||||||
-- A tuple:
|
-- A tuple:
|
||||||
("haskell", 1)
|
("haskell", 1)
|
||||||
|
|
||||||
@ -106,11 +107,13 @@ add a b = a + b
|
|||||||
-- Using the function
|
-- Using the function
|
||||||
add 1 2 -- 3
|
add 1 2 -- 3
|
||||||
|
|
||||||
-- You can also put the function name between the two arguments with backticks:
|
-- You can also put the function name between the two arguments
|
||||||
|
-- with backticks:
|
||||||
1 `add` 2 -- 3
|
1 `add` 2 -- 3
|
||||||
|
|
||||||
-- You can also define functions that have no characters! This lets you define
|
-- You can also define functions that have no characters! This lets
|
||||||
-- your own operators! Here's an operator that does integer division
|
-- you define your own operators! Here's an operator that does
|
||||||
|
-- integer division
|
||||||
(//) a b = a `div` b
|
(//) a b = a `div` b
|
||||||
35 // 4 -- 8
|
35 // 4 -- 8
|
||||||
|
|
||||||
@ -135,12 +138,13 @@ foo (x, y) = (x + 1, y + 2)
|
|||||||
map func [x] = [func x]
|
map func [x] = [func x]
|
||||||
map func (x:xs) = func x:(map func xs)
|
map func (x:xs) = func x:(map func xs)
|
||||||
|
|
||||||
-- Anonymous functions are created with a backslash followed by all the arguments.
|
-- Anonymous functions are created with a backslash followed by
|
||||||
|
-- all the arguments.
|
||||||
map (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]
|
map (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]
|
||||||
|
|
||||||
-- using fold (called `inject` in some languages) with an anonymous function.
|
-- using fold (called `inject` in some languages) with an anonymous
|
||||||
-- foldl1 means fold left, and use the first value in the array as the initial
|
-- function. foldl1 means fold left, and use the first value in the
|
||||||
-- value for the accumulator.
|
-- array as the initial value for the accumulator.
|
||||||
foldl1 (\acc x -> acc + x) [1..5] -- 15
|
foldl1 (\acc x -> acc + x) [1..5] -- 15
|
||||||
|
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
@ -210,7 +214,7 @@ haskell = if 1 == 1
|
|||||||
then "awesome"
|
then "awesome"
|
||||||
else "awful"
|
else "awful"
|
||||||
|
|
||||||
-- case statements: Here's how you could parse command line arguments in Haskell
|
-- case statements: Here's how you could parse command line arguments
|
||||||
case args of
|
case args of
|
||||||
"help" -> printHelp
|
"help" -> printHelp
|
||||||
"start" -> startProgram
|
"start" -> startProgram
|
||||||
@ -280,3 +284,6 @@ qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
|
|||||||
```
|
```
|
||||||
|
|
||||||
Haskell is easy to install. Get it [here](http://www.haskell.org/platform/).
|
Haskell is easy to install. Get it [here](http://www.haskell.org/platform/).
|
||||||
|
|
||||||
|
You can find a much gentler introduction from the excellent [Learn you a Haskell](http://learnyouahaskell.com/)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user