F#とPythonとVimScriptとかとか

こんにちは！`callmekohei`ですっ！

F#と他の言語のちょっとした比較表を作ってみました！

![IMG_4088.jpg](https://qiita-image-store.s3.amazonaws.com/0/149269/fde210e1-057e-d7fb-01ed-f650d499ff60.jpeg)





### operator
|               | F#                    | python | vim script | MQL4 |
|---------------|:---------------------:|:------:|:----------:|:----:|
| assign	    | <-                    | =      | =          | =    |
| equal	        | =                     | ==     | ==         | ==   |
| not equal	    | <>                    | !=     | !=         | !=   |
| same instance	| object.referenceEqual | is     | is         |      |
| and	        | &&                    | and    | &&         | &&   |
| or	        | ll                  | or     | ll	      | ll |
| not	        | not                   | not    | !          | !    |

### The ternary operator
F#

```fsharp
stdout.WriteLine(if true then "foo" else "bar")
// foo
```

Python

```python
print('foo' if True else 'bar')
# foo
```
Vim script

```vim
echo( v:true ? 'foo' : 'bar')
" foo
```
MQL4

```mq4
void OnStart()
{
    printf( true ? "foo" : "bar");
}
// foo
```

### Commnet
F#

```fsharp
// line comment

(*
    block comment
*)
```
Python

```python
# line commnet

# block comment is nothing
```

Vim script

```vim
" line comment

" block comment is nothing
```

MQL4

```mq4
// line commnet

// block comment is ???
```

### Strings
FSharp

```fsharp
// string concatenation
let foobar = "foo" + "bar"
stdout.WriteLine(foobar) // foobar

// verbatim strings
let verbatimXml = @"<book title=""foo bar"">"
let tripleXml = """<book title="foo bar">"""
stdout.WriteLine(verbatimXml) // <book title="foo bar">
stdout.WriteLine(tripleXml)   // <book title="foo bar">

// strings indent
let poem =
    "foo\n\
     bar\n\
     baz"

stdout.WriteLine(poem)
// foo
// bar
// baz
```
Python

```python
# string concatenation
foobar = 'foo' + 'bar'
print(foobar) # foobar

# TODO
# verbatim strings
verbatimXml = r'<book title="foo bar">'
tripleXml   = """<book title="foo bar">"""
print(verbatimXml) # <book title="foo bar">
print(tripleXml)   # <book title="foo bar">

# strings indent
poem ="foo\n\
bar\n\
baz"
print(poem)
# foo
# bar
# baz

```
Vim script

```vim
" string concatenation
let foobar = 'foo' . 'bar'
echo(foobar)
"foobar

" TODO
" verbatim strings
let verbatimXml = "<book title=""foo bar"">"
let tripleXml = '<book title="foo bar">'
echo(verbatimXml)
" <book title=
echo (tripleXml)
" <book title="foo bar">

"TODO
" strings indent
let poem = 'foo\n
            \bar\n
            \baz'
echo(poem)
" foo\nbar\nbaz
```

MQL4

```mq4
void OnStart()
{
    // string concatenation
    string foobar = "foo" + "bar";
    printf( foobar );
    // foobar

    // verbatim strings
    // ???

    // strings indent
    // ???
}
```

### Variables
F#

```fsharp
let mutable foo = 123
```
Python

```python
foo = 123
```
vim script

```vim
let foo = 123
```
MQL4

```mq4
int foo = 123;
```

### Constant
F#

```fsharp
let foo = 123
```

Python

```python
# nothing
```

Vim script

```vim
let foo = 123 | lockvar foo
```

MQL4

```mq4
#define FOOBAR "foobar"
```

### Confirm type

F#

```fsharp
"foo".GetType()
|> stdout.WriteLine
// System.String
```

Python

```python
print(type('foo'))
# <class 'str'>
```

Vim script

```vim
echo(type('abc'))
" 1

" :help type
" --------------------------------------------
" Number:       0  v:t_number
" String:       1  v:t_string
" Funcref:      2  v:t_func
" List:         3  v:t_list
" Dictionary:   4  v:t_dict
" Float:        5  v:t_float
" Boolean:      6  v:t_bool (v:false and v:true)
" None          7  v:t_none (v:null and v:none)
" Job           8  v:t_job
" Channel       9  v:t_channel
```

MQL4

```mq4

```


### Condition ( if )

F#(short-circuit evaluation)

```fsharp
if true || false then
    stdout.WriteLine("aaa")
else
    stdout.WriteLine("bbb")

// aaa
```

Python(short-circuit evaluation)

```python
if True or False:
    print('aaa')
else:
    print('bbb')

# aaa
```

Vim script(short-circuit evaluation)

```vim
if v:true || v:false
    echo("aaa")
else
    echo("bbb")
endif

" aaa
```

MQL4(short-circuit evaluation)

```mq4
void OnStart()
{
    if ( true || false )
        printf("aaa");
    else
        printf("bbb");
}
// aaa
```

### Condition ( switch )

F#

```fsharp
let foo = 123

match foo with
| 123 -> stdout.WriteLine("aaa")
| _   -> stdout.WriteLine("bbb")

// aaa
```

Python

```python
# nothing
```

Vim script

```vim
" nothing
```

MQL4

```mq4
void OnStart()
{
    int foo = 123;
    switch( foo )
    {
        case 123 : printf("aaa"); break;
        default  : printf("bbb"); break;
    }
}
// aaa
```

### Loop ( for )

F#

```fsharp
for n in 1..5 do
    stdout.WriteLine(n)

// 1
// 2
// 3
// 4
// 5
```

Python

```python
for n in range(1,6):
    print(n)

# 1
# 2
# 3
# 4
# 5
```

Vim script

```vim
for n in range(1,5)
    echo(n)
endfor

" 1
" 2
" 3
" 4
" 5
```

MQL4

```mq4
void OnStart()
{
    for(int n = 1; n <= 5 ; n++)
        printf( DoubleToStr(n) );
}
// 1.00000000
// 2.00000000
// 3.00000000
// 4.00000000
// 5.00000000
```


### Function

F#

```fsharp
let foo a b =
    a + b

stdout.WriteLine(foo 1 2)

// 3
```

Python

```python
def foo(a,b):
    return a + b

print(foo(1,2))

# 3
```

Vim script

```vim
function! s:foo(a,b)
    return a:a + a:b
endfunction

echo(s:foo(1,2))
" 3


" :h variables ---> 3. Internal variable
" -----------------------------------------------------------------
" buffer-variable     b:  Local to the current buffer.
" window-variable     w:  Local to the current window.
" tabpage-variable    t:  Local to the current tab page.
" global-variable     g:  Global.
" local-variable      l:  Local to a function.
" script-variable     s:  Local to a |:source|'ed Vim script.
" function-argument   a:  Function argument (only inside a function).
" vim-variable        v:  Global, predefined by Vim.
```

MQL4

```mq4
int foo (int a , int b)
{
    return ( a + b );
}

void OnStart()
{
    printf ( DoubleToStr(foo(1,2)));
}
// 3
```

### Class

F#

```fsharp
type Foo(a,b) =
    
    // initial constructor
    let x = a + 100
    let y = b + 100
    
    // add method
    member this.Bar(z) =
        x + y + z

(Foo(1,2)).Bar(3)
|> stdout.WriteLine

// 206
```

Python

```python
class Foo:

    # initial constructor
    def __init__(self,a,b):
        self.x = a + 100
        self.y = b + 100

    # add method
    def bar(self,c):
        return self.x + self.y + c

print(Foo(1,2).bar(3))

# 206
```

Vim script

```vim
function! s:foo(a,b)

    " initial constructor
    let self =  {}
    let self.x = a:a + 100
    let self.y = a:b + 100

    " add method
    function! self.bar(z)
        return self.x + self.y + a:z
    endfunction

    return self
endfunction

echo(s:foo(1,2).bar(3))

" 206
```

MQL4

```mq4
#property strict

class Foo
{
    private:
        int x, y;

    public:

        // initial constructor
        Foo(int a , int b);

        // add method
        int bar(int c)
        {
            return(x + y + c);
        }
};

Foo::Foo(int a ,int b)
{
    x = a + 100;
    y = b + 100;
}


void OnStart()
{
    Foo foo(1,2);
    printf( DoubleToStr( foo.bar(3)) );
}
// 206.00000000
```

### Exception handling

Fsharp

```fsharp
try
    stdout.WriteLine(1/0)
with e -> stdout.WriteLine(e.Message)

// Attempted to divide by zero.
```

Python

```python
try:
    print(1/0)
except Exception as e:
    print(e)

# division by zero
```

Vim script

```vim
" TODO
try
    echo(1/0)
catch
    echo(v:exception)
endtry

" 9223372036854775807
```

MQL4

```mq4
// nothing...
```

### File handling

FSharp

```fsharp
open System.IO

let filePath = "./callmekohei.txt"

do
    // open file
    // 'use' will call Dispose method of IDisposable interface, and Dispose of StreamReader = reader.Close()
    use stream = new StreamReader(filePath)

    // read file
    while not stream.EndOfStream do
        stream.ReadLine() |> stdout.WriteLine

    // implicitly closed at end-of-scope by use


// call
// me
// kohei
```

Python

```python
filePath = ('./callmekohei.txt')

# open file
f = open(filePath)

# read file
line = f.readline()

while line:
    print (line)
    line = f.readline()

# close file
f.close


# call
#
# me
#
# kohei
```

Vim script

```vim
let filePath = "./callmekohei.txt"

" open file , read file and close file
let lst = readfile(filePath)

for s in lst
    echo(s)
endfor

" call
" me
" kohei
```

MQL4

```mq4
void OnStart()
{
    // For security reasons, the file is opened in the folder of the client terminal in the subfolder MQL4\files
    // e.g : C:\Program Files\OANDA - MetaTrader\MQL4\Files\callmekohei.txt
    int filehandle = FileOpen("callmekohei.txt", FILE_TXT);
    string s = FileReadString(filehandle);
    printf(s);
    FileClose(filehandle);
}
```