How to use (import (prefix ...)) - chicken-scheme

I'm trying to figure out how to use the
> (import (prefix some-module :some-module))
docs are here
I found the example of the definition here.
Now, how do I then refer to a definition on it?
Here are some things I tried:
:some-module.baz
some-module.baz
:some-module:baz
some-module:baz
None of them worked

I got an answer in the #chicken irc room.
The example I saw with the colon in front of the prefix is wrong. It should be:
> (import (prefix some-module some-module:))
Then to use:
> (some-module:baz)

Related

How to import module in scheme?

Im new to scheme. I am trying to import module "sorting" in scheme. I tried everything from (load sorting) to (open sorting), (import sorting). I was able to use
,open sorting
when I am in scheme bash. However I want to import the module to a scheme file. I am using scheme48
You need to use the module language.
More details can be found here: http://community.schemewiki.org/?scheme48-module-system
Basically, instead of writing just a normal scheme file, foo.scm:
;; foo.scm
(define (hello) (display "Hello World!"))
You need to use the module language
;; foo2.scm
;; this is not scheme, it's the module language
(define-structure hello (export hello)
(open scheme)
;; or others here
(begin
;; this is Scheme
(define (hello) (display "Hello World!"))))
You can learn more about the module language here: http://s48.org/1.8/manual/manual-Z-H-5.html

Common Lisp ltk 'button' class not found

I'm learning Common Lisp (Clozure CL) on the Mac and installed quicklisp, with the help of a generous contributor on here. The 'ltk' library works when running (ltk::ltk-eyes) or (ltk:ltktest).
Running (ql:quickload "ltk") seems to work as it return the following:
Load 1 ASDF system:
ltk
; Loading "ltk"
I have a problem running the following code taken from the 'ltk' documentation. Here's the script:
(ql:quickload "ltk") ;my addition to the script
(defun hello-1()
(with-ltk ()
(let ((b (make-instance 'button
:master nil
:text "Press Me"
:command (lambda ()
(format t "Hello World!~&")))))
(pack b))))
Howver, when I run (hello-1) I get this:
Error: Class named BUTTON not found.
While executing: FIND-CLASS, in process Listener(4).
Type cmd-/ to continue, cmd-. to abort, cmd-\ for a list of available restarts.
If continued: Try finding the class again
Type :? for other options.
My guess is that the 'ltk' library is not properly accessed in the function definition? I tried to fix the problem by using ltk:with-ltk as it seems to be a ltk function.
(defun hello-1()
(ltk:with-ltk ()
(let ((b (make-instance 'button
:master nil
:text "Press Me"
:command (lambda ()
(format t "Hello World!~&")))))
(pack b))))
But that produced the following error. It seems that I'm getting closer at fixing it since the 2D canvas also appeared with the GUI alerting me of the error.
Thanks for your help.
Common Lisp manipulates symbols, which belong to packages. The Lisp reader is responsible for resolving an unqualified reference to a symbol to the actual, qualified symbol. That depends on the current package being bound to *PACKAGE* when reading code. As suggested in comments, you should read ยง21. Programming in the Large: Packages and Symbols from P. Seibel's Practical Common Lisp.
You can define your own package as follows:
(defpackage :test-ltk
(:use :cl :ltk))
The :use clause is the declarative equivalent of USE-PACKAGE. The above makes the test-ltk package inherit all external symbols from the Common Lisp and LTK packages. Generally, you cannot using too many packages together because you are more likely to have conflicts: two symbols belonging to different packages but having the same name cannot be accessed in an unqualified way. This is a bit like in C++, where you are discouraged from doing using namespace std.
In order to selectively import some symbols and not others, you use :import-from instead. For example, you could define the preceding package as follows:
(defpackage :test-ltk
(:use :cl)
(:import-from :ltk #:with-ltk #:button #:pack))
Here, you only list the 3 symbols you are actually accessing. The #:symbol notation represents uninterned symbols, i.e. symbols that belongs to no package and are used only for their names. You could have used strings (in uppercase) instead.
Then, you change the current package with IN-PACKAGE. The unqualified access to symbols are resolved according to the current package's definitions:
(in-package :test-ltk)
(defun hello-1 ()
(with-ltk ()
(pack
(make-instance 'button
:master nil
:text "Press Me"
:command (lambda () (format t "Hello World!~&"))))))
Solution (thanks #jkiiski for referring to the : operator) : After carefully reading the error messages in the console I was able to resolve the problem. The compiler was not able to access the 'ltk' functions (with-ltk ...) and (pack ...) as well as the button class.
The corrected code is below (using Quicklisp to use the ltk library):
(load #P"/Users/myDirectory/quicklisp/setup.lisp")
(ql:quickload "ltk")
(defun hello-1()
(ltk:with-ltk ()
(let ((b (make-instance 'ltk:button
:master nil
:text "Press Me"
:command (lambda ()
(format t "Hello World!~&")))))
(ltk:pack b))))
My next step is to see if I can find a similar method to 'using namespace' in C++ so that I don't need to keep using let: , and simplify the code.

Emacs company-mode completion not working

I just began using emacs a few days ago and I've been having trouble setting up company-mode. I installed company-mode using package-install and added the following in my .emacs file :-
(require 'company)
(add-hook 'after-init-hook 'global-company-mode)
Yet, when I go over to haskell-mode, company-mode completion doesn't pop up at all (likewise with racket-mode) when I wait for a few seconds on a keyword. Could I have installed something that could have been messing up or conflicting with company-mode?
Also, when I try to invoke company-complete manually, it just says "no completion found".
Edit: Tried out auto-complete as an alternative and code completion does not work when I press tab, but when i invoke auto-complete on a word, it works.
Edit2: got auto-complete to work.
https://i.imgur.com/Vn4f2GX.png
It looks like flyspell-mode was conflicting with auto-complete. But, No luck with getting company mode to work.
Really appreciate any help.
I got completion to work for company-mode in haskell without much hassle.
All I had to do was add company backends to my .emacs :-
(add-to-list 'company-backends 'company-dabbrev-code)
(add-to-list 'company-backends 'company-yasnippet)
(add-to-list 'company-backends 'company-files)
And get the package intero which adds company-mode support for haskell. Like so :-
(add-hook 'haskell-mode-hook 'company-mode)
(add-hook 'haskell-mode-hook 'intero-mode))
As for racket-mode, I add this in my .emacs :-
(defun my-racket-mode-hook ()
(set (make-local-variable 'company-backends)
'((company-capf company-dabbrev-code)))
(company-quickhelp-mode 0))
(add-hook 'racket-mode-hook 'my-racket-mode-hook)
(add-hook 'racket-mode-hook 'company-mode)
(add-hook 'racket-repl-mode-hook 'my-racket-mode-hook)
(add-hook 'racket-repl-mode-hook 'company-mode)

Emacs shell script mode hook

For some reason my shell script mode hooks do not get executed. Example in my .emacs:
(add-hook 'shell-script-mode-hook (lambda ()
(rainbow-delimiters-mode 1)))
causes the variables to be set, but the mode is not loaded for the opened script files. What is the proper way to hook here?
I use the default shell script mode (modeline says e.g. Shell-script[bash]). Do I have to hook for each shell type individually (sh, bash, zsh)? If yes can you please tell me how?
Thank you very much!
EDIT3:
It turned out to be due a conflict of textmate-mode with the skeleton-pair-insert in sh-mode (I tried to avoid the conflict by disabling textmate in sh-mode, which then left the sh-mood-hook aparatus in ruins. I've removed textmate-mode completely and use now the standard skeleton-pair approch globaly.
I'll accept phils answer - without him I'd probably not be able to debug this on my own.
EDIT2:
Thanks to phils, I think his comment takes us closer to solution. It's not a problem with rainbow-delimiters though. I removed all sh-mode-hook except your hello message one and restart Emacs. When I open a .sh file I get this:
Setting up indent for shell type bash
setting up indent stuff
Indentation variables are now local.
Indentation setup for shell type bash
File mode specification error: (void-function nil)
Note no "hello" message. The value of sh-mode-hook is:
(nil
(lambda nil
(message "hello")))
I think the problem is this first nil value - though I don't see that it would be set anywhere.
If I eval this:
(setq sh-mode-hook t)
(add-hook 'sh-mode-hook (lambda () (message "hello")))
I see the hello message, though after restart (I've put those lines in .emacs) it is gone again (the nil is again on top of the hook).
Any idea what to do to have active hook at setup?
EDIT1:
I've tried also:
(add-hook 'sh-mode-hook (lambda ()
(rainbow-delimiters-mode 1)))
with same negative result - not sure if this is relevant though...
shell-script-mode is an alias for sh-mode. I haven't checked, but I would suspect that only the hook variable for the 'real' function name is evaluated, so I think sh-mode-hook would be the one to use.
Anyhow, there's nothing broken about your syntax, so there may be something amiss with the use of (rainbow-delimiters-mode 1). For instance, you should be able to observe that the following works correctly:
(add-hook 'sh-mode-hook (lambda () (message "hello")))
FWIW, for hooks I recommend not using anonymous functions at all, simply because it's much easier to update your hook function if it is named (removing the old lambda expression from the variable before adding an updated one is just annoying in my books).
Try to remove ':
(add-hook 'shell-script-mode-hook (lambda () (rainbow-delimiters-mode 1)))

How do you load a file into racket via command line?

I have been trying to launch a racket program from the commandline (via 'racket') but have not been having success. According to the documentation (here http://docs.racket-lang.org/reference/running-sa.html#%28part._mz-cmdline%29) passing -f followed by a file should evaluate that file. However, I can't seem to get this to work. As a test, I made the following file:
;test.rkt
#lang racket
(define a 1)
Then, running it in racket (supposedly loading the file) and attempting to recall the value of a:
racket -f test.rkt -i
Welcome to Racket v5.1.1.
> a
reference to undefined identifier: a
My end goal is to be able to launch a different program from a shell script using the --main option combined with loading the definitions with -f to start up execution, just have become a bit baffled since I can't seem to get this trivial bit working.
Removing the #lang line works, but it means that your code is no longer a module, which makes it a pretty bad idea. To start racket on a given module file, all you need is to just run racket on the file, nothing else is needed. For example, put this in test.rkt:
#lang racket/base
(printf "Hi\n")
and just run it with racket test.rkt. If you want to have command-line flags, you can use (current-command-line-arguments) to get a vector of additional command-line arguments, but there's also the racket/cmdline library that makes it much easier to have standard kinds of flag processing. Here's an example for that:
#lang racket/base
(require racket/cmdline)
(define excitedness "")
(define mode "Hi")
(command-line
#:multi
[("-e" "--excited") "add excitedness levels"
(set! excitedness (string-append excitedness "!"))]
#:once-each
[("-b" "--bye") "turn on \"bye\" mode"
(set! mode "Bye")])
(printf "~a~a\n" mode excitedness)
and you can now run it with racket test.rkt <flags>. See also the Racket Guide's section on scripts for making your test.rkt even easier to run.
Finally, there is the --main approach that you've seen -- to use that, your module needs to provide a main function that receives all the command-line flags as arguments. For example:
#lang racket/base
(require racket/string)
(provide main)
(define (main . xs)
(printf "You gave me ~s flags: ~a\n"
(length xs) (string-join xs ", ")))
and to run it:
racket -t /tmp/y -m -- foo bar baz
The flag breakdown is: -t requires your module, -m causes racket to run your main function, and -- means that the following flags are all passed to your program. You can combine the flags like so:
racket -tm- /tmp/y foo bar baz
and that would be something that you'd usually put in your script trampoline as described in that guide section.
And, of course, this is all described in great details in the reference manual.
Remove the #lang racket header from your file:
;test.rkt
(define a 1)

Resources