This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grain/fib): take index from cmdline and stdin
Signed-off-by: Roman Volosatovs <[email protected]>
- Loading branch information
1 parent
642458a
commit 073b01f
Showing
1 changed file
with
23 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,27 @@ | ||
let rec fibonacci = (n) => { | ||
if (n == 0 || n == 1) { | ||
n | ||
import {toList} from "array" | ||
import {drop, forEach, length, map} from "list" | ||
import {parseInt} from "number" | ||
import {trim} from "string" | ||
import {fdRead, stdin} from "sys/file" | ||
import {argv} from "sys/process" | ||
import {expect} from "result" | ||
|
||
let rec fibonacci = (i) => { | ||
if (i <= 1) { | ||
i | ||
} else { | ||
fibonacci(n - 1) + fibonacci(n - 2) | ||
fibonacci(i - 1) + fibonacci(i - 2) | ||
} | ||
} | ||
|
||
print(fibonacci(7)) | ||
let args = expect("failed to parse arguments", argv()) | ||
let indexes = drop(1, toList(args)) | ||
let indexes = if (length(indexes) == 0) { | ||
print("No arguments specified, please specify Fibonacci sequence index: ") | ||
let (s, _) = expect("failed to read stdin", fdRead(stdin, 19)) | ||
let i = expect("failed to parse stdin as integer number", parseInt(trim(s), 10)) | ||
[i] | ||
} else { | ||
map((arg) => { expect("failed to parse argument " ++ toString(arg) ++ " as integer number", parseInt(arg, 10)) }, indexes) | ||
} | ||
forEach((i) => { print("Fibonacci sequence number at index " ++ toString(i) ++ " is " ++ toString(fibonacci(i)) ++ "\n") }, indexes) |