Generate a random integer in Idris - idris

How can one generate a random integer in Idris 1.3.3?
Example program that doesn't work:
module Random
import Effect.Random --This gets me access to the rndInt function
I run the program as follows:
idris random.idr -p effects
Trying the rndInt function in the REPL, I get the following error message:
*random> rndInt 1 10
(input):Can't infer argument m to rndInt
Please include the full source code in your answer. Thank you.

Elaborating on Alissa Tung's answer, here's the full working source code, including setting the random seed (srand) with the system time:
module Random
import Effect.Random
import Effects
import System
main : IO ()
main = do
t <- time
n <- run $ do
srand t
rndInt 1 100
putStrLn $ show n
Run with:
idris random.idr -p effects
Output:
*random> :exec main
88
*random> :exec main
96
*random> :exec main
19

Related

How to resolve the issue "Can't find import Effects" in Idris?

I was trying to compile the following "hello world" source file after some time away from Idris using a nix-shell environment (direction):
module Main
import Effects
import Effect.StdIO
hello : Eff () [STDIO]
hello = putStrLn "Hello world!"
main : IO ()
main = run hello
My output was as follows during the experimentation; despite specifying what seems to be the right library, I still get the same error at the end:
[nix-shell:~/workspace/idris_tmp]$ idris --listlibs
00prelude-idx.ibc
prelude
00base-idx.ibc
base
[nix-shell:~/workspace/idris_tmp]$ exit
exit
brandon#brandon-750-170se-DevContainer:~/workspace/idris_tmp
$ nix-shell -p 'idrisPackages.with-packages (with idrisPackages; [ contrib effects ])' -p closurecompiler
these derivations will be built:
/nix/store/j44rkb0fpxqd4qkamrg1ysf9kbx1q2qa-idris-1.3.0.drv
these paths will be fetched (1.55 MiB download, 4.37 MiB unpacked):
/nix/store/1fcldlyp5n0ry22dsqfam68mpra6jky9-idris-effects-1.3.0
/nix/store/a9rjm84pbmvg6dmkdzhl9q1wliyi0q4b-idris-contrib-1.3.0
copying path '/nix/store/a9rjm84pbmvg6dmkdzhl9q1wliyi0q4b-idris-contrib-1.3.0' from 'https://cache.nixos.org'...
copying path '/nix/store/1fcldlyp5n0ry22dsqfam68mpra6jky9-idris-effects-1.3.0' from 'https://cache.nixos.org'...
building '/nix/store/j44rkb0fpxqd4qkamrg1ysf9kbx1q2qa-idris-1.3.0.drv'...
/nix/store/5dny6qnjfq9liya5z1sxvr2g64bqypwl-idris-base-1.3.0/nix-support:
propagated-build-inputs: /nix/store/1fcldlyp5n0ry22dsqfam68mpra6jky9-idris-effects-1.3.0/nix-support/propagated-build-inputs
/nix/store/a9rjm84pbmvg6dmkdzhl9q1wliyi0q4b-idris-contrib-1.3.0/nix-support:
propagated-build-inputs: /nix/store/1fcldlyp5n0ry22dsqfam68mpra6jky9-idris-effects-1.3.0/nix-support/propagated-build-inputs
/nix/store/a5x52wi84jgjiimpnkfpcl3mbpbkf1r4-idris-1.3.0/nix-support:
propagated-build-inputs: /nix/store/1fcldlyp5n0ry22dsqfam68mpra6jky9-idris-effects-1.3.0/nix-support/propagated-build-inputs
[nix-shell:~/workspace/idris_tmp]$ idris --listlibs
00effects-idx.ibc
effects
00base-idx.ibc
base
00prelude-idx.ibc
prelude
00contrib-idx.ibc
contrib
[nix-shell:~/workspace/idris_tmp]$ idris --codegen javascript hello.idr -o hello.js
Can't find import Effects
Try to add -p effects to your commandline as in
idris -p effects --codegen javascript hello.idr -o hello.js
this will load the additional library/package. You can avoid doing this by defining an ipkg file, which describes your build. See here: http://docs.idris-lang.org/en/latest/reference/packages.html

The PAKCS REPL consider something undefined, but the module defining it is loaded

The problem
My code includes a module called Tests which defines the following:
broken :: SetRBT Int
broken = insertRBT 1 $ emptySetRBT (<)
I can evaluate broken in the REPL:
All> broken
RedBlackTree.RedBlackTree (_impl#==#Prelude.Eq#Prelude.Int) (_impl#==#Prelude.Eq#Prelude.Int) (_def#<#Prelude.Ord (_inst#Prelude.Ord#Prelude.Int)) (RedBlackTree.Tree RedBlackTree.Black 1 RedBlackTree.Empty RedBlackTree.Empty)
All>
I cannot, however, evaluate the RHS of broken's definition:
All> insertRBT 1 $ emptySetRBT (<)
PAKCS_Main_Exp.curry, line 3.18: Error:
Undefined type RedBlackTree.RedBlackTree
ERROR occurred during parsing!
All>
Or so I believed, until I tried attaching a type signature:
All> insertRBT 1 $ emptySetRBT (<) :: SetRBT Int
RedBlackTree.RedBlackTree (_impl#==#Prelude.Eq#Prelude.Int) (_impl#==#Prelude.Eq#Prelude.Int) (_def#<#Prelude.Ord (_inst#Prelude.Ord#Prelude.Int)) (RedBlackTree.Tree RedBlackTree.Black 1 RedBlackTree.Empty RedBlackTree.Empty)
All>
I'm fine with having to attach type signatures when needed, or more generally, to do whatever the error messages suggest I should do. But how would I know to interpret the above error message as meaning "you've got to attach a type signature"? More generally, what does that type error even mean, given that (see below) RedBlackTree is loaded?
What I am loading
Each time I start PAKCS I run :l All. That loads a module which reads, in relevant part,
module All ( module M
) where
import FiniteMap as M
import SetRBT as M
import RedBlackTree as M
import Tests as M
That idiom lets me load all the things that I need (there are others) without producing a long prompt.
If I run :modules it looks like RedBlackTree should be defined:
All> :modules
Currently loaded modules:
All (loaded from ./.curry/pakcs/All.pl)
Prelude (loaded from /home/jeff/logic/curry/install/pakcs-2.0.2/lib/.curry/pakcs/Prelude.pl)
FiniteMap (loaded from /home/jeff/logic/curry/install/pakcs-2.0.2/lib/.curry/pakcs/FiniteMap.pl)
SetRBT (loaded from /home/jeff/logic/curry/install/pakcs-2.0.2/lib/.curry/pakcs/SetRBT.pl)
RedBlackTree (loaded from /home/jeff/logic/curry/install/pakcs-2.0.2/lib/.curry/pakcs/RedBlackTree.pl)
Tests (loaded from ./.curry/pakcs/Tests.pl)
All>
You must tell the interpreter to add referenced modules.
All> :add RedBlackTree SetRBT
... some messages ...
All SetRBT RedBlackTree> insertRBT 1 $ emptySetRBT (<)
and it will work.

Fortran runtime error: End of file when reading input data

I'm currently running a code and I'm always getting to the same end. I am trying to read an input file and it returns the error:
Fortran runtime error: End of file
In an other post they said to put in the iostat specifier so now my code looks like this:
INTEGER :: m
INTEGER :: st
Open(Unit = 13,action='read',file='Data_Inp.dat',status='old')
read (13,*, iostat = st) m
write (*,*) st
write (*,*) m
ALLOCATE(winkel(m),energie(m))
Do i = 1,m
read(13,*),winkel(i),energie(i)
End Do
And the input file looks like this:
12
-17.83 -0.019386527878
-15.83 -0.020125057233
-12.83 -0.020653853148
-11.83 -0.020840036028
-9.83 -0.020974157405
-8.83 -0.021056401707
-6.83 -0.021065517811
-5.83 -0.020992571816
-4.83 -0.020867828448
-1.83 -0.02069158012
Now the terminal prints a -1 for iostat and a constantly changing number for m.
If the first read command is causing an error, check for extraneous characters before or after "12" in your input file, especially if you created it on one platform (Windows?) and using it on another platform (Linux? Mac?)

Is it possible to implement a break statement using a user input in python

time=0
stop=input()
while time<1000000000000000000000000000000000000000000000000000:
if stop==input("999"):
break
print (time)
time= time+1
print("time taken is",time)
This is a program for an average speed camera. I was wondering whether it is possible for the while loop to stop when the user inputs "999". The value at which the code is broken would then be the new content of the time variable.
It's a bit unclear of what you're trying to accomplish, but based on the code you provided and your question, it sounds like you want to measure how long it takes for someone to enter a specific value. You can modify: Python - Infinite while loop, break on user input:
#guess_999.py
import sys
import os
import fcntl
import time
fl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, fl | os.O_NONBLOCK)
time_started = time.time()
while True:
try:
stdin = sys.stdin.read()
if "999" in stdin:
print "It took %f seconds" % (time.time() - time_started)
break
except IOError:
pass
Then running it:
$ python guess_999.py
$ 6
$ 999
$ It took 2.765054 seconds
EDIT: PO wanted to do something completely different. I refer to Mark's answer.
You messed it up a little bit ;)
Do:
answer = input("type something")
if answer == "999":
break
Explanation:
- input() will return a string of what the user typed into the console. What you write into the brackets is what will be written on the line when you are asked to type something. This is usually a question like "what's your name?"
- if the answer is "999", the command break will be executed => loop stops

Microwind does not create my equation in compile

I user microwind and try to build the mask from the compile one line choice but it wont let me the equation simplified is ~A&(~B|~C|D)
Try bisecting the equation into small parts and then try.
part1
Get a layout of only ~B | ~C | D . save that as module1
part2
Import module1 and go for its implementation for ~A & *module1*
Follow the D flip Flop tutorial and post the efforts done.