forked from perks/pumpkin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.pk
43 lines (34 loc) · 846 Bytes
/
demo.pk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def reduce(func: (Int, Int => Int), acc: Int, l: List[Int]): Int =>
if(is_empty(l)):
acc
else:
reduce(func, func(hd(l), acc), tl(l))
def map(f: (Int => Int), l: List[Int]): List[Int] =>
if(is_empty(l)):
l
else:
f(hd(l))::(map(f, tl(l)))
def even(n: Int): Bool =>
if(n % 2 is 0):
True
else:
False
val x = [1,2,3,4] |> map((x:Int => x + 5 :Int)) |> reduce((x: Int, y: Int => x + y : Int), 0) |> even
print("Example 1: Functional Programming Styles [see source]")
print(x)
print("\nExample 2: Functional Composition [see source]")
def gcd(a : Int, b : Int) : Int =>
if(b is 0):
a
else:
gcd(b, a % b)
def relativePrimes(a: Int) =>
if (a is 1):
True
else:
False
val p = relativePrimes << gcd
if(p(25, 15)):
print("You have relative primes")
else:
print("Not relative primes")