Newbie playing with haskell stack scripting and turtle.
Created a folder stack-scripts. Thought if it looks good, then I'll create some haskell utils for me to replace bash scripts.
Created a file named turtle.hs with following text:
#!/usr/bin/env stack
-- stack --resolver lts-11.2 script
{-# LANGUAGE OverloadedStrings #-}
import Turtle
main = echo "Hello!"
made the chmod +x turtle.hs and tried to execute it.
Got the following error message:
turtle.hs:1:1: error:
File name does not match module name:
Saw: `Main'
Expected: `Turtle'
|
1 | #!/usr/bin/env stack
| ^
It does what it should if I rename turtle.hs to turtle.sh. But then I have no syntax highlighting for haskell.
Also it works if I rename it to something-other.hs. But then Haskero (VSCode) complains about import Turtle line: Couldn't guess that module name. Does it exist?
What I'm missing here? Running in git bash on Windows.
Apparently you need to give the script a different name as the module name in which the code runs, will automatically be derived from it and now it will conflict with the imported Turtle module. Renaming it to turtlescript.hs and then
#!/usr/bin/env stack
-- stack --resolver lts-11.2 script --package turtle
{-# LANGUAGE OverloadedStrings #-}
import Turtle
main :: IO ()
main = echo "Hello!"
worked for me.
Related
I'm writing my first transcrypt code (python transpiled to JS for the browser -- what a great idea!) and I'm stuck already, translating this JS code into python:
import { saveAs } from 'file-saver';
import inspect from 'browser-util-inspect';
As you can see, both those third-party modules (which are in my node_modules/ dir, from npm) have hyphens in their name. But it's not legal python to say
from node_modules.file-saver import saveAs
(because python module names have to be identifiers) so I'm not sure how to proceed.
It should be possible to use module names containing a -, using aliases, but it was broken with TS 3.7.1
It'll be fixed in the next minor release, an example will be included in the autotest.
Sorry for the inconvenience.
TS 3.7.8 is out, containing a fix for this.
[EDIT]
# __pragma__ ('alias', 'specific_module_a', 'specific-module-a')
# __pragma__ ('alias', 'S', '$')
# __pragma__ ('alias', 'test_modules_b', 'test-modules-b')
# __pragma__ ('alias', 'specific_module_b', 'specific-module-b')
from test_modules_a.__specific_module_a import the__S__Function
from test_modules_b__.__specific_module_b import theBFunction as aBFunction
the__S__Variable = 3
print (the__S__Variable)
the__S__Function (print)
aBFunction (print)
Note that the pragma's are needed to conform original Python syntax and Python scanner/parser, that don't allow $ and - in names.
I am fairly new to Bash scripting and am trying to set environment and libraries to use and then build a particular script. Here is the Bash script I wrote:
#! /bin/bash
export DISPLAY :0.0
#setenv LD_LIBRARY_PATH /usr/local/szip2.1/lib:${LD_LIBRARY_PATH}
# above line should be equivalent to
export LD_LIBRARY_PATH "/usr/lib64:/usr/lib:$LD_LIBRARY_PATH"
module load Szip/2.1-gmpolf-2015
module load cuda/6.5
#while ( 1 )
cd /home/sbansal6/misr-stereo-2013.11.24
/home/sbansal6/misr-stereo-2013.11.24/bin/misr-stereo /home/sbansal6/ellipsoids/0034007 AN AA
#end
And here is what I get as the error:
[sbansal6#cg-gpu01 STEREO]$ ./mstdrv.csh
./mstdrv.csh: line 3: export: `:0.0': not a valid identifier
./mstdrv.sh: line 6: export: `/usr/lib64:/usr/lib:/sw/cuda/6.5/lib64:/sw/EasyBuild/software/Szip/2.1-gmpolf-2015/lib:/sw/EasyBuild/software/ScaLAPACK/2.0.2-gmpich-2015-OpenBLAS-0.2.14-LAPACK-3.5.0/lib:/sw/EasyBuild/software/FFTW/3.3.4-gmpich-2015/lib:/sw/EasyBuild/software/OpenBLAS/0.2.14-GCC-4.9.2-binutils-2.25-LAPACK-3.5.0/lib:/sw/EasyBuild/software/MPICH/3.1.4-GCC-4.9.2-binutils-2.25/lib:/sw/EasyBuild/software/GCC/4.9.2-binutils-2.25/lib/gcc/x86_64-unknown-linux-gnu/4.9.2:/sw/EasyBuild/software/GCC/4.9.2-binutils-2.25/lib64:/sw/EasyBuild/software/GCC/4.9.2-binutils-2.25/lib:/sw/binutils-2.25/lib': not a valid identifier
/home/sbansa16/misr-stereo-2013.11.24/bin/misr-stereo:error while loading shared libraries: libglut.so.3: cannot open shared object file: No such file or directory
I am basically trying to add the libraries from Szip and Cuda in my environment and then build a C++ program which is at the path I wrote later. I do understand that the last error that I got which said error while loading shared libraries: libglut.so.3 is because I don't have that library in my libraries.
I am sorry if this question is trivial. I appreciate any help. Thanks in advance.
UPDATE:
I have also tried using setenv from csh but it is still not working.
Bash syntax for setting an environment variable would be:
export NAME=value
or
NAME=value
export NAME
I have a Main.hs file with two functions.
Module Main where
import Data.List
main :: IO()
main = interact reverse
functionThatWorks = putStrLn "Ajax"
After I set the directory and load Main.hs I have no problem with calling functionThatWorks.
Except when I want to take a text file as input like so:
Main<in.txt or ./Main<in.txt
I get an error saying 'parse error on input 'in' '
Does anyone know I can make this work in the Terminal?
p.s. I use a Mac.
Unfortunately ghci doesn't understand input redirection like the shell does.
I would suggest running your program with runhaskell:
runhaskell Main.hs < in.txt
This is a bit long, so bear with me!
I'm having a bit of trouble working with a Haskell program, that I have to use as part of a uni project. For reference, it's Casper.
So, you're supposed to execute a script, which is actually a Bash script to invoke Hugs interpreter like this:
exec $HUGSBIN/hugs $HUGSARGS +p"Casper> " $FILES
Where $FILES points to a Main.lhs file.
After this, I need to invoke a function "compile" with a path to a file, in the interpreter.
I need to perform the above in a scripted manner. I need this automated because I'm writing a program that will call on Casper in the background.
So I compiled the .lhs file. Now I want to execute the "compile" function but I have no idea how this is done. I try:
./Main compile <a path>
from the command line but it returns me an error about a file "test" not found. Upon investigation, I see these lines in the Main.lhs file:
>main :: String -> IO()
>main = compile "test"
>compile :: String -> IO()
>compile s = catch (compile0 False s) handler
[...snipped]
The 2nd line solves this question. Now my question is, how do I invoke the "compile" function and pass a path to it after I have compiled main.lhs? From the interpreter, I just type "compile " and it works, but I can't get the same to work after compiling the main.lhs and executing from the command line? Any ideas why? Is there any way I can script Hugs if all else fails?
Thank you for any assistance!
You may access the command-line arguments passed to a Haskell program via getArgs. For example, it sounds like you want a main function that does something like this:
>main = do
> args <- getArgs
> case args of
> [] -> putStrLn "What file did you want me to compile?"
> [filename] -> compile filename
> _ -> putStrLn "I only compile one file at a time."
Modify to taste.
Replace main with
main = getArgs >>= \(arg1:_) -> compile arg1
This will pass the first command line argument (arg1) to compile instead of "test", and ignore the rest (_). You may need to add
import System
or
import System.Environment
I can't remember what is needed in hugs for this.
On a fresh install of Haskell Platform for Max OSX, the following code fails on import Test.HUnit when run using the runghc interpreter.
{--
- Save this file as Main.hs and run with % runghc Main.hs
-}
module Main where
import Test.HUnit
derp = test [ "a silly test" ~: 'a' ~=? 'a' ]
tests = TestList [ derp ]
main::IO()
main = (runTestTT tests) >>= (\x -> putStrLn $ show x)
However, when using ghci, doing a simple import Test.HUnit works just fine.
How can I resolve this discrepancy between ghc from the command line and the ghci REPL?
Reboot the machine. Not sure whether it's paths not containing the right directories or something else, but a fresh start after install seems to have resolved it.