LoginSignup
2
0

More than 5 years have passed since last update.

C++を使いながらHaskell学習

Posted at

備忘録も兼ねて ٩(ˊᗜˋ*)و.

配列の中の数を足す

hairetsuplus.cpp
#include<iostream>
#include<vector>
using namespace std;

int main(){
    vector<int> xs{1,2,3};
    vector<int> ys;
    for(auto x:xs){
        ys.emplace_back(x+10);
    }

    for(auto y:ys){
        cout<<y<<endl;
    }
}

11
12
13

hairetsuplus.hs
main = print $ map(+10)[1,2,3]

[11,12,13]

配列の合計値を算出

wa.cpp
#include<iostream>
#include<vector>
using namespace std;

int main(){
    vector<int> xs{1,2,3};
    int total = 0;
    for(auto x:xs){
        total += x;
    }
    cout<<total<<endl;
}

6

wa.hs
main = print $ foldr (+) 0 [1,2,3]

6

配列のある数字以上の数を取り出す

bignumberinsert.cpp
#include<iostream>
#include<vector>
using namespace std;

int main(){
    vector<int> xs{1,2,3,4,5,6};
    vector<int> ys;
    for(auto x:xs){
        if(x>3){
            ys.emplace_back(x);
        }
    }   

    for(auto y:ys){
        cout<<y<<endl;
    }
}

4
5
6

bignumberinsert.hs
main = print $ filter(>3)[1,2,3,4,5,6]

[4,5,6]

関数をかけあわせる

kansuu.cpp
#include<iostream>
#include<vector>
using namespace std;

int f(int x){return x+10;}

int g(int x){return x*x;}

int main(){
    vector<int>xs{1,2,3};
    vector<int>ys;
    for(auto x:xs){
        ys.emplace_back(g(f(x)));
    }

    for(auto y:ys){
        cout<<y<<endl;
    }
}

121
144
169

kansuu.hs
f x = x+10

g x = x * x

main = print $ map(g.f)[1,2,3]

[121,144,169]

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0