Shen tutorial example for typed function does not compile - shen

I just discovered the Shen language and so I installed it under my debian linux as an SBCL build and started to work through the Shen in 15 minutes tutorial.
I made it up to the point, where you turn on type checking, using
Shen, copyright (C) 2010-2015 Mark Tarver
www.shenlanguage.org, Shen 22.2
running under Common Lisp, implementation: SBCL
port 3.0.3 ported by Mark Tarver, Robert Koeninger and Bruno Deferrari
(tc +)
(define total
(list number) --> number
[] -> 0
[X | Y] -> (+ X (total Y)))
Entering the above sequence in the repl or storing it into a .shen file and then (load "in15min.shen") yields the unexpected:
[list number] is not a legitimate constructor
and trying to evaluate it anyway, I get a type error as output.
So here my questions:
Is the tutorial outdated and the syntax has changed?
Or do I do something wrong?

The rendering from the style sheet has missed {}s.
(define total
{(list number) --> number}
[] -> 0
[X | Y] -> (+ X (total Y)))
is right.
Mark

Related

Procedures doesn't give any output (scheme)

Im using emacs and mzscheme as interpreter
Welcome to Racket v8.2 [cs]
> (define (square a) (* a a))
> square 4
#<procedure:square>
> 4
I just finished setting up emacs with mzscheme, combinations are working well but my procedures doesn't seem to work. Tried everything i could find.

Can't execute the scala^z3 fibonacci example

I am trying to use Scala^Z3 on MacOS 64 bit, with the following settings:
res0: String = Z3 4.0 (build 0, rev. 0), ScalaZ3 3.2.c (in dev.)
Now when I try to execute the fibonacci example from the ScalaZ3 main page, I get the following output, and then the execution halts:
fib0 ::: (= (fib!0 0) 0)
fib1 ::: (= (fib!0 1) 1)
fibN ::: (forall (k!0 Int)
(implies (> k!0 1) (= (fib!0 k!0) (+ (fib!0 (- k!0 1)) (fib!0 (- k!0 2)))))
:pat {(fib!0 k!0)})
Query ::: (= x (fib!0 1))
I waited for some hours, but there is nothing more happening ;)
Does anybody have any ideas, what I am doing wrong? Too new version of scala or z3?
Regards,
Florian
(Note that we haven't had a chance to test Scala^Z3 with the latest version of Z3 yet. Thanks for breaking ground here.)
The problem you're trying to solve contains universal quantifiers and is satisfiable. Past version of Z3 would terminate with unknown and you could ask for a tentative model (as shown on the documentation page to which you linked). It could very well be that the newer version is set up to try really hard to disprove the formula rather than return unknown, and thus does not terminate.
You can try an unsatisfiable query, which should work better. Try for instance:
(= (fib 10) (fib 12))
Z3 should be able to instantiate the quantifiers sufficiently many times to conclude that formula is unsatisfiable.

pretty printing and expansion

EDIT I'm not just asking about 'indenting' every single line of code but about "pretty printing" and/or a wider definition of "indenting", where lines would be grouped/split depending on what they contain.
Here's a function (which I wrote to solve an Euler problem but that is not the point: I could have written it differently) which I'm trying to pretty print:
Version manually indented:
(apply max
(flatten
(for [x (range 100 1000)]
(map
#(if (= (str (* x %)) (apply str (reverse (str (* x %)))))
(* x %)
0)
(range x 1000)))))
Apparently this isn't the one true Lisp indentation/printing style and, as I'd like to get good habits from the start, I tried to "pretty print" it, doing the following from Emacs' slime-repl clojure REPL:
user> (doc pprint) ;; just to show which version of pprint I'm using
clojure.pprint/pprint
...
So I tried to do:
user> (pprint '(apply max
(flatten
(for [x (range 100 1000)]
(map
#(if (= (str (* x %)) (apply str (reverse (str (* x %)))))
(* x %)
0)
(range x 1000))))))
And I got the following:
(apply
max
(flatten
(for
[x (range 100 1000)]
(map
(fn*
[p1__13958#]
(if
(=
(str (* x p1__13958#))
(apply str (reverse (str (* x p1__13958#)))))
(* x p1__13958#)
0))
(range x 1000)))))
As I understand it the #(...) notation for the lambda is a "reader macro". So how can I pretty print without triggering the expansion of macros? More generally: how can I pretty print "what I see in my text editor"?
Also, is it normal that nearly everything goes on a new line? For example:
(apply
max
(flatten
(for
[x (range 100 1000)]
(map
...
seems to be a waste of quite some vertical screen real estate. Is this the way Lisp code should be indented?
And a last related question: can Emacs be configured to "pretty print" a selected region or an entire .clj buffer?
There's no "official" way to indent lisp code, but I like the way clojure-mode indents clojure code (see below).
I don't know why pprint prints code the way it does but according to this page, it's an early release that isn't feature-complete yet.
For the emacs indentation part, you might want to take a look at clojure-mode.
The closest I have seen to a consensus is to "indent it like Emacs does", I suppose to be pollitically correct I should include the corollary "indent it like vim does". the other editors like eclipse+CCW seem to match this fairly closely. though the default pretty printer adds more newlines.
Here's what I'm currently doing:
(defn pprint-code [code]
(with-pprint-dispatch code-dispatch
(binding [*print-suppress-namespaces* true]
(pprint code))))
NOTE: Look in clojure.pprint to resolve the free-vars.
It's certainly an improvement. I still don't exactly know how to tweak *print-miser-width* and/or *print-right-margin* to get better output. YMMV. My current beef is with (auto-)gensyms - I want them demangled whilst pprinting in certain contexts.
Ideally, forms/functions could define their own formatting rules, e.g. let-bindings should always be on separate lines. I'm not aware of being able to do this with clojure.core.
For reference, see this overview.

SDRAW is not working in my computer

i have installed clisp in my ubuntu machine. I am trying to use sdraw to display the cons structure. But this SDRAW is not working in my computer.I tried to use the examples given in the book A gentle indtroduction to symbolic computation. Can anybody tell me how can i enable it to work?
[edit]
i used following command
(sdraw ' (alpha (barvo) charlie))
i got following message
*** - EVAL: undefined function SDRAW
Thanks
yes i finally got the answer. we need to load the file sdraw.generic and we can have to load the file using the command.
>(load "sdraw.generic")
then use the sdraw command to display the cons art.
>BreakBreak 40 [45]> (sdraw '(1 2 (10)))
[*|*]--->[*|*]--->[*|*]--->NIL
| | |
v v v
1 2 [*|*]--->NIL
|
v
10
You can run the generic version of sdraw as follow
CL-USER> (load "sdraw.generic")
CL-USER> (sdraw:sdraw '(a b c (d e) f))
That will produce output to terminal/REPL
But if you want the fancier or GUI version, sdraw.gui
CL-USER> (ql:quickload :clx)
CL-USER> (load "sdraw.gui")
CL-USER> (sdraw:sdraw '(a b (c d (e) f)))
With fancy result

Lisp function call error

I've written a Lisp function like this:
(defun power (base exponent)
(if (= exponent 0)
1
(* base (power (- exponent 1)))))
When I try to call it, however, I get some errors:
CL-USER 2 > (power 2 3)
Error: POWER got 1 arg, wanted at least 2.
1 (abort) Return to level 0.
2 Return to top loop level 0.
Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.
CL-USER 3 : 1 > (power 2)
Error: POWER got 1 arg, wanted at least 2.
1 (abort) Return to level 1.
2 Return to debug level 1.
3 Return to level 0.
4 Return to top loop level 0.
Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.
CL-USER 4 : 2 > (power 2 3 4)
Error: POWER got 3 args, wanted 2.
1 (continue) Ignore the extra arg.
2 (abort) Return to level 2.
3 Return to debug level 2.
4 Return to level 1.
5 Return to debug level 1.
6 Return to level 0.
7 Return to top loop level 0.
Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.
What's going on here? If I give it two arguments, it thinks I gave it one. If I give it three, it thinks I gave it three. If I give it one, it thinks I gave it one...
It is the recursive call that only has one argument:
(power (- exponent 1))
It should be like this:
(power base (- exponent 1))
The recursive call is your problem. You forgot to pass the base in as the first argument.
(* base (power (- exponent 1)))))
should be:
(* base (power base (- exponent 1)))))
Compile your functions. In LispWorks use c-sh-c to compile a definition in the editor.
Here in the REPL:
CL-USER 18 > (defun power (base exponent)
(if (= exponent 0)
1
(* base (power (- exponent 1)))))
POWER
CL-USER 19 > (compile 'power)
;;;*** Warning in POWER: POWER is called with the
;;; wrong number of arguments: Got 1 wanted 2
The Compiler will already tell you that there is a problem with the code.
Note that the LispWorks Listener (the REPL) does not compile. You have to compile the definitions you enter at the Listener with the function COMPILE. Alternatively you can type the definitions into an editor window and compile it from there (by either compiling the file, the buffer or the expression). LispWorks also has features to locate code where an error happens.
Lisp comes with the function expt, so no need to define your own.
(Unless this is an exercise or homework, in which case you might want to look at more efficient methods, such as exponentiation by squaring.)