Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Program
- open System
- let inc = fun x -> x + 1
- let abs = fun x -> if x >= 6 then x else -x
- let rec addTo =
- fun n ->
- if n = 0 then
- 0
- else
- n + (addTo (n - 1))
- //! There is another syntax for writing addTo
- let rec addToAlt n =
- if n = 0 then
- 0
- else
- n + (addTo (n - 1))
- // Part 4 - Making an operator, mind the order in math * before +
- let (>==) = fun x y -> x + y * x
- //TODO: Exercise 1
- let rec allNumber n =
- if n = 0 then
- "0"
- else
- (allNumber (n - 1)) + " " + (string n)
- // TODO: Exercise 2
- let rec allNumberRev n =
- if n = 0 then
- "0"
- else
- (string n) + " " + (allNumberRev (n - 1))
- // TODO: Exercise 3
- let rec allNumberRange l h =
- if l = h + 1 then
- ""
- else
- (string l) + " " + (allNumberRange (l + 1) h)
- // TODO: Exercise 4
- let rec allNumberRangeRev l h =
- if l = h then
- (string l)
- else
- (allNumberRangeRev (l + 1) h) + " " + (string l)
- // TODO: Exercise 5
- let rec allEvenRange lower upper =
- if lower = upper then
- if lower % 2 = 0 then
- (string lower)
- else
- ""
- else
- let current =
- if lower % 2 = 0 then
- (string lower)
- else
- ""
- current + " " + (allEvenRange (lower + 1) upper)
- // TODO: Exercise 6
- let rec drawLine n =
- if n = 0 then
- ""
- else
- (drawLine (n - 1)) + "*"
- // TODO: Exercise 7
- let rec drawSymbols char length =
- if length = 0 then
- ""
- else
- (drawSymbols char (length - 1)) + char
- // TODO: Exercise 8
- let rec toBinary n =
- if n < 0 then
- "Incorrect parameter."
- else
- if n = 0 then
- ""
- else
- (toBinary (n / 2)) + (string (n % 2))
- // TODO: Exercise 9
- //! Don't understand the question..
- [<EntryPoint>]
- let main argv =
- printfn "Running the program.."
- //* Part 1
- let resBig = (fun x -> x + 1) 1
- // Same as
- let res = inc 1
- printfn "%d" resBig
- printfn "%d" res
- //* Part 2
- let num = abs 5
- let num2 = abs -10
- printfn "abs func %d and %d" num num2
- //* Part 3
- let add = addTo 5
- let addalt = addToAlt 10
- printfn "the addTo func %d and the addToAlt func %d" add addalt
- //* Part 4 - Operator
- let op = 5 >== 3
- printfn "My result is %d" op
- printfn "\n\n----Exercises ------------------------\n\n"
- let exercise n =
- printfn "Exercise %d" n
- //TODO: Exercise 1
- exercise 1
- let num = allNumber 10
- printfn "My result is %s" num
- printfn "\n"
- //TODO: Exercise 2
- exercise 2
- let num = allNumberRev 10
- printfn "My result is %s" num
- printfn "\n"
- //TODO: Exercise 3
- exercise 3
- let num = allNumberRange 16 23
- printfn "My result is %s" num
- printfn "\n"
- //TODO: Exercise 4
- exercise 4
- let num = allNumberRangeRev 16 23
- printfn "My result is %s" num
- printfn "\n"
- //TODO: Exercise 5
- exercise 5
- let num = allEvenRange 4 10
- printfn "My result is %s" num
- printfn "\n"
- //TODO: Exercise 6
- exercise 6
- let line = drawLine 10
- printfn "My result is %s" line
- printfn "\n"
- //TODO: Exercise 7
- exercise 7
- let line = drawSymbols "&" 5
- printfn "My result is %s" line
- printfn "\n"
- //TODO: Exercise 8
- exercise 8
- let num = toBinary 34
- printfn "My result is %s" num
- printfn "\n"
- //TODO: Exercise 9
- //! Don't understand the question..
- 0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement