For a project I need the output of a Lisp program, however I have never heard of Lisp before and I cannot make it run. I have downloaded the file (.lsp) from a website and looked at it with a texteditor to understand its logic. The lisp file gets a certain input and reduces that input through some iterative steps. I want to know not only the output but also the iteratrion process, which is why a logfile of that would be great. I have downloaded Portacle and some other Lisp interpreters/compilers, but I cannot make it work. It is so frustrating. Is there a simple way to run a lisp file from a directory? I have watched several tutorials and read thorugh some threads here, but I have still no clue how to run the Lisp file like I would run a Python file.
I am so immensely grateful for any help! I am pretty sure there is a simple way that I am just not seeing.
Thank you so much in advance!
If you have to spend some time working on this, it is worth installing at least Quicklisp and Slime, following this tutorial:
https://lisp-lang.org/learn/getting-started/
You can however start with only the interpreter. You need to install sbcl, and I recommend having rlwrap too because SBCL prompt does not have fancy readline features.
$ rlwrap sbcl
Then, you'll be in a Lisp REPL. Execute this to ensure the code is compiled with the maximum level of debugging:
* (sb-ext:restrict-compiler-policy 'debug 3)
The environment should reply with:
((DEBUG . 3))
NIL
Then, you can compile your input file (use an absolute path, or a path relative to the directory where you stared sbcl):
* (compile-file "/tmp/concepts")
A lot of text will be emitted, but the interpreter replies with the name of the object file being produced (e.g. "/tmp/concepts.fasl"), you can load it by supplying * as an argument, since the asterisk means the last result.
* (load *)
Following the comments in the file (between #| and |#), you can test it as follows:
* (test-concepts shepard)
This performs a lot of tests.
You can trace individual functions if you want with (trace X Y Z) where X , Y and Z are function names (you can also untrace them).
To simplify, here is a list where all (I think) symbols described in the comments are traced:
(trace frequency-of-properties
frequency-of-pairs
count-of-n
simplify-eight
remove-non-minimals
replace-lis-in-concept
print-linear-models
simplify-quads
make-master-list
make-pairs-for-quads
find-pair-quads
memberprop-lis
max-length
count-of-n
frequency-of-pairs
find-pairs-that-simplify
find-pair-to-simplify
remove-non-minimals
triples
three-pairs
simplify-by-mates
doubles
co
remove-non-minimals
replace-lis-in-concept
simplify-by-mates
singles
remove-non-minimals
replace-lis-in-concept
simplify-by-mates
print-linear-models
simplify-by-mates)
Using the same test as above, the last test prints the following trace:
Problem 6 0: (PRINT-LINEAR-MODELS (((A) (B) (- C)) ((A) (- B) (C)) ((- A) (B) (C)) ((- A) (- B) (- C))))
AB-C A-BC -ABC -A-B-C
0: PRINT-LINEAR-MODELS returned NIL
0: (FREQUENCY-OF-PROPERTIES (((A) (B) (- C)) ((A) (- B) (C)) ((- A) (B) (C)) ((- A) (- B) (- C))))
0: FREQUENCY-OF-PROPERTIES returned
((((A)) 2) (((B)) 2) (((- C)) 2) (((- B)) 2) (((C)) 2) (((- A)) 2))
0: (COUNT-OF-N ((((A)) 2) (((B)) 2) (((- C)) 2) (((- B)) 2) (((C)) 2) (((- A)) 2)) 4)
1: (COUNT-OF-N ((((B)) 2) (((- C)) 2) (((- B)) 2) (((C)) 2) (((- A)) 2)) 4)
2: (COUNT-OF-N ((((- C)) 2) (((- B)) 2) (((C)) 2) (((- A)) 2)) 4)
3: (COUNT-OF-N ((((- B)) 2) (((C)) 2) (((- A)) 2)) 4)
4: (COUNT-OF-N ((((C)) 2) (((- A)) 2)) 4)
5: (COUNT-OF-N ((((- A)) 2)) 4)
6: (COUNT-OF-N NIL 4)
6: COUNT-OF-N returned NIL
5: COUNT-OF-N returned NIL
4: COUNT-OF-N returned NIL
3: COUNT-OF-N returned NIL
2: COUNT-OF-N returned NIL
1: COUNT-OF-N returned NIL
0: COUNT-OF-N returned NIL
0: (SIMPLIFY-BY-MATES (((A) (B) (- C)) ((A) (- B) (C)) ((- A) (B) (C)) ((- A) (- B) (- C))))
1: (MAX-LENGTH (((A) (B) (- C)) ((A) (- B) (C)) ((- A) (B) (C)) ((- A) (- B) (- C))))
2: (MAX-LENGTH (((A) (- B) (C)) ((- A) (B) (C)) ((- A) (- B) (- C))) 3)
3: (MAX-LENGTH (((- A) (B) (C)) ((- A) (- B) (- C))) 3)
4: (MAX-LENGTH (((- A) (- B) (- C))) 3)
5: (MAX-LENGTH NIL 3)
5: MAX-LENGTH returned 3
4: MAX-LENGTH returned 3
3: MAX-LENGTH returned 3
2: MAX-LENGTH returned 3
1: MAX-LENGTH returned 3
0: SIMPLIFY-BY-MATES returned NIL
No simplification possible. Number of models 4
This should give a better understanding of what is happening.
You can also use (step (test shepard)), in which case the execution breaks in the debugger; use help to have more information about the commands you can invoke. For example you can write STEP (or just S) to step into an expression, NEXT to skip over an expression and go to the next one, OUT to step out of the current frame, etc.
Related
(define ( addposition x )
(cond
[(empty? x) "empty list"]
[#t (for/list ([i x])
(list i (add1 (index-of x i))))]
))
(addposition (list 'a 'b 'c ))
it returns me '((a 1) (b 2) (c 3)), but I need the list like '(a 1 b 2 c 3)
As a bare minimum to get what you want you can throw that nested list to a (flatten) call:
> (flatten '((a 1) (b 2) (c 3)))
'(a 1 b 2 c 3)
But overall the idea to build mini lists with index-of and then flattening it is not the most performant. Nor will it be correct if your list contains duplicate values.
If we keep our own record of the next index, and using recursion instead of the otherwise handy for/list structure, we can build our list this way:
(define (add-positions xs [ind 0])
(if (null? xs)
xs
(append (list (first xs) ind)
(add-positions (rest xs) (add1 ind))
)))
(add-positions '(a b c d))
;=> '(a 0 b 1 c 2 d 3)
This can be expressed pretty naturally using map and flatten:
;;; Using map and flatten:
(define (list-pos xs (start 0))
(flatten (map (lambda (x y) (list x y))
xs
(range start (+ start (length xs))))))
Here map creates a list of lists, each containing one value from the input list and one value from a range list starting from start, and flatten flattens the result.
This seems more natural to me than the equivalent using for/list, but tastes may differ:
;;; Using for/list:
(define (list-pos xs (start 0))
(flatten (for/list ((x xs)
(p (range start (+ start (length xs)))))
(list x p))))
There are a lot of ways that you could write this, but I would avoid using append in loops. This is an expensive function, and calling append repeatedly in a loop is just creating unnecessary overhead. You could do this:
;;; Using Racket default arguments and add1:
(define (list-pos xs (pos 0))
(if (null? xs)
xs
(cons (car xs)
(cons pos (list-pos (cdr xs) (add1 pos))))))
Here the first element of the list and a position counter are added onto the front of the result with every recursive call. This isn't tail recursive, so you might want to add an accumulator:
;;; Tail-recursive version using inner define:
(define (list-pos xs (start 0))
(define (loop xs pos acc)
(if (null? xs)
(reverse acc)
(loop (cdr xs)
(add1 pos)
(cons pos
(cons (car xs) acc)))))
(loop xs start '()))
Because the intermediate results are collected in an accumulator, reverse is needed to get the final result in the right order.
You could (and I would) replace the inner define with a named let. Named let should work in Racket or Scheme; here is a Scheme version. Note that Scheme does not have default arguments, so an optional argument is used for start:
;;; Tail-recursive Scheme version using named let:
(define (list-pos xs . start)
(let loop ((xs xs)
(pos (if (null? start) 0 (car start)))
(acc '()))
(if (null? xs)
(reverse acc)
(loop (cdr xs)
(add1 pos)
(cons pos
(cons (car xs) acc))))))
All of the above versions have the same behavior:
list-pos.rkt> (list-pos '(a b c))
'(a 0 b 1 c 2)
list-pos.rkt> (list-pos '(a b c) 1)
'(a 1 b 2 c 3)
Here is a simple solution using for/fold
(define (addposition l)
(for/fold ([accum empty]) ([elem l])
(append accum elem)))
I love the for loops in Racket 😌
Note: As pointed out by ad absurdum, append is expensive here. So we can simply reverse first and then use cons to accumulate
(define (addposition l)
(for/fold ([accum empty]) ([elem (reverse l)])
(cons (first elem) (cons (second elem) accum))))
As others have pointed out, you can start by making a list of lists. Let's use a list comprehension:
> (for/list ([x '(a b c)]
[pos (in-naturals 1)])
(list x pos))
'((a 1) (b 2) (c 3))
Here, we iterate in parallel over two sets of data:
The list '(a b c)
The stream (in-naturals 1), which produces 1, 2, 3, ....
We combine them into lists with list, giving this structure:
'((a 1) (b 2) (c 3))
This is called "zipping", and using list comprehensions is a convenient way to do it in Racket.
Next, we want to flatten our list, so it ends up looking like this:
'(a 1 b 2 c 3)
However, you shouldn't use flatten for this, as it flattens not just the outermost list, but any sub-lists as well. Imagine if we had data like this, with a nested list in the middle:
> (flatten
(for/list ([x '(a (b c d) e)]
[pos (in-naturals 1)])
(list x pos)))
'(a 1 b c d 2 e 3)
The nested list structure got clobbered! We don't want that. Unless we have a good reason, we should preserve the internal structure of each element in the list we're given. We'll do this by using append* instead, which flattens only the outermost list:
> (append*
(for/list ([x '(a (b c d) e)]
[pos (in-naturals 1)])
(list x pos)))
'(a 1 (b c d) 2 e 3)
Now that we've got it working, let's put it into a function:
> (define (addposition xs)
(append*
(for/list ([x xs]
[pos (in-naturals 1)])
(list x pos))))
> (addposition '(a b c))
'(a 1 b 2 c 3)
> (addposition '(a (b c d) e))
'(a 1 (b c d) 2 e 3)
Looks good!
I need to write procedure for calculation of weighted sum in follow functionality:
((weighted-sum 1) 5)
5
((weighted-sum 1/2 1/2) 3 1)
2
etc..
So far I did only how to get parameters for procedure:
(define (weighted-sum x . xn) (cons x xs))
(weighted-sum 2 3)
> '(2 3)
How to get ((weighted-sum 2 3) X X) parameters?
Thank you.
Your question doesn't have one easy answer. It sounds like you're supposed to write a function that accepts a sequence of weights, and returns a function that accepts a sequence of weights, and sums the products of the weights and the sums (by the way, stating this yourself would have been really helpful...).
1) Is this your design, or someone else's? I would not design this function this way.
2) You can write functions that return functions in a bunch of different ways. E.g.:
;; these all do the same thing.
;; they all have the type (number -> (number -> number))
(define a (lambda (x) (lambda (y) (+ x y))))
(define ((a x) y) (+ x y))
(define (a x)
(define (b y) (+ x y))
b)
So weighted-sum takes a variable number of values as parameters (let's call them ws) , and returns a new procedures that, in its turn, takes a variable number of parameters (vs) and does the calculation.
In racket, the for/fold construct comes in handy:
(define (weighted-sum . ws)
(lambda vs
(for/fold ((res 0)) ((i (in-list ws))
(j (in-list vs)))
(+ res (* i j)))))
or even
(define ((weighted-sum . ws) . vs)
(for/fold ((res 0)) ((i (in-list ws))
(j (in-list vs)))
(+ res (* i j))))
Alternatively, using a more classic foldl returning a named inner procedure:
(define (weighted-sum . ws)
(define (sub . vs)
(foldl
(lambda (i j res) (+ res (* i j)))
0
ws
vs))
sub)
For any of those:
> ((weighted-sum 1) 5)
5
> ((weighted-sum 1/2 1/2) 3 1)
2
This is my lisp code.
(DEFUN F (A B)
(SETF C (* 4 A))
(SETF D (* 2 (EXPT B 3)))
(SETF RES (+ C D))
(IF (AND (TYPEP A 'INTEGER) (TYPEP B 'INTEGER))
(list 'Final 'value '= res)
'(YOUR INPUTS ARE NOT NUMBERS)))
For example, (f 5 9) works well.
But (f 'w 'q) doesn't work with the following error message:
(ERROR TYPE-ERROR DATUM W EXPECTED-TYPE NUMBER FORMAT-CONTROL
~#<~s' is not of the expected type~s'~:#> FORMAT-ARGUMENTS
(W NUMBER))
Error: W' is not of the expected typeNUMBER'
I want to make if A,B is integer calculate 4A+2B^3.
Else if at least one is not an integer print error message.
I try to the code shown above.
But how can I make this error handling using if statements?
First, you should use LET or LET* to define local variables.
(defun f (a b)
(let* ((c (* 4 a)) ; You need LET* instead of LET because
(d (* 2 (expt b 3))) ; RES depends on the previous variables.
(res (+ c d)))
(if (and (typep a 'integer) (typep b 'integer))
(list 'final 'value '= res)
'(your inputs are not numbers))))
The actual problem is that you're doing the calculations before you check that the arguments are integers. You should move the calculation inside the IF.
(defun f (a b)
(if (and (integerp a) (integerp b))
(let* ((c (* 4 a))
(d (* 2 (expt b 3)))
(res (+ c d)))
(list 'final 'value '= res))
'(your inputs are not numbers)))
Returning lists like that is kind of strange. If you intend them as output for the user, you should instead print the messages and return the actual result.
(defun f (a b)
(if (and (integerp a) (integerp b))
(let ((result (+ (* 4 a)
(* 2 (expt b 3)))))
(format t "Final value = ~a~%" result)
result) ; Return RESULT or
(format t "Your inputs are not integers.~%"))) ; NIL from FORMAT.
In most cases you should signal an error if the arguments are not correct type. Printing output from a function that does the calculation is usually a bad idea.
(defun f (a b)
(check-type a integer "an integer")
(check-type b integer "an integer")
(+ (* 4 a)
(* 2 (expt b 3))))
(defun main (a b)
(handler-case
(format t "Final value = ~a~%" (f a b))
;; CHECK-TYPE signals a TYPE-ERROR if the type is not correct.
(type-error () (warn "Your inputs are not integers."))))
(main 12 1)
; Final value = 50
;=> NIL
(main 12 'x)
; WARNING: Your inputs are not integers.
;=> NIL
Common Lisp condition system also allows you to use restarts to fix errors. CHECK-TYPE establishes a restart named STORE-VALUE, which you can invoke to supply a correct value for the place. In this case it probably doesn't make sense, but you could do something like use 1 as a default.
(defun main (a b)
(handler-bind ((type-error (lambda (e)
(store-value 1 e))))
(format t "Final value = ~a~%" (f a b))))
(main 12 1)
; Final value = 50
;=> NIL
(main 12 'x)
; Final value = 50
;=> NIL
Notice that conditions/error handlers do add some overhead, so for performance critical functions you might not want to use them and instead check your arguments before calling the function.
(declaim (ftype (function (integer integer) integer) f))
(defun f (a b)
(declare (optimize (speed 3) (safety 0) (debug 0)))
(+ (* 4 a)
(* 2 (expt b 3))))
(defun main (a b)
(if (and (integerp a)
(integerp b))
(format t "Final value = ~a~%" (f a b))
(warn "Your inputs are not integers.")))
I am using the book SICP and attempting to solve this exercise:
1.2.4 Exponentiation
Exercise 1.18. Using the results of exercises 1.16 and 1.17, devise
a procedure that generates an iterative process for multiplying two
integers in terms of adding, doubling, and halving and uses a
logarithmic number of steps
I am trying to solve this with the following code:
(define (double x)
(+ x x))
(define (halve x)
(floor (/ x 2)))
(define (* a b)
(define (iter count accumulate)
(cond ((= count 1) accumulate)
((even? a) (iter (halve count) (+ accumulate (double b))))
(else empty)))
(iter a 0))
As you might see, I am trying to deal with even numbers first.
I am using the SICP wiki as my solutions-guide. They suggest some tests to see if the code works:
(* 2 4)
(* 4 0)
What I do not get is that my code passes on these two first tests, dealing only with even numbers.
However, when I try some big numbers which are multiples of two, the code fails. I checked the result using Python. For instance,
(IN PYTHON)
2**100
>> 1267650600228229401496703205376
2**98
>> 316912650057057350374175801344
a = 2**100
b = 2**98
a*b
>> 401734511064747568885490523085290650630550748445698208825344
When I use my function inside Dr. Racket with these values I get a different result:
(* 1267650600228229401496703205376 316912650057057350374175801344)
My result is: 63382530011411470074835160268800, which is wrong, as Python built-in functions suggest.
Why this is happening?
The recursive step seems wrong, and what's that empty doing there? also, what happens if b is negative? this solution should work:
(define (mul a b)
(define (iter a b acc)
(cond ((zero? b) acc)
((even? b) (iter (double a) (halve b) acc))
(else (iter a (- b 1) (+ a acc)))))
(if (< b 0)
(- (iter a (- b) 0))
(iter a b 0)))
For example:
(mul 1267650600228229401496703205376 316912650057057350374175801344)
=> 401734511064747568885490523085290650630550748445698208825344
(define (sum-two-sqrt a b c)
(cond ((and (<= c a) (<= c b)) sqrt-sum(a b))
((and (<= a b) (<= a c)) sqrt-sum(b c))
((and (<= b a) (<= b c)) sqrt-sum(a c))
)
)
(define (sqrt-sum x y)
(+ (* x x) (*y y))
)
(define (<= x y)
(not (> x y))
(sum-two-sqrt 3 4 5)
This is my code
Please help me to fix the problem. :)
I just start studing Lisp today.
learned some C before but the two language is QUITE DIFFERENT!
This is the question
Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
If you have better algorithm
POST IT!
Thank you :)
There's no need to define <=, it's a primitive operation. After fixing a couple of typos:
sqrt-sum: you were incorrectly invoking the procedure; the opening parenthesis must be written before the procedure name, not after.
sqrt-sum: (*y y) is incorrect, you surely meant (* y y); the space(s) after an operator matter.
This should work:
(define (sqrt-sum x y)
(+ (* x x) (* y y)))
(define (sum-two-sqrt a b c)
(cond ((and (<= c a) (<= c b)) (sqrt-sum a b))
((and (<= a b) (<= a c)) (sqrt-sum b c))
((and (<= b a) (<= b c)) (sqrt-sum a c))))
Or another alternative:
(define (sum-two-sqrt a b c)
(let ((m (min a b c)))
(cond ((= a m) (sqrt-sum b c))
((= b m) (sqrt-sum a c))
(else (sqrt-sum a b)))))
Following up on a suggestion by #J.Spiral and seconded by #River, the following Racket code reads nicely to me:
#lang racket
(define (squares-of-larger l)
(define two-larger (remove (apply min l) l))
(for/sum ([i two-larger]) (* i i)))
(squares-of-larger '(3 1 4)) ;; should be 25
Please note that this solution is entirely functional, since "remove" just returns a new list.
Also note that this isn't even in the same neighborhood with HtDP; I just wanted to express this concisely, and show off for/sum.
I didn't have Scheme interpreter here, but below seems to be shorter then other suggestions :) So it's in CL, but should look very similar in Scheme.
(defun sum-two-sqrt (a b c)
(let ((a (max a b))
(b (max (min a b) c)))
(+ (* a a) (* b b))))
In Scheme this would translate to:
(define (sum-two-sqrt a b c)
(let ((a (max a b))
(b (max (min a b) c)))
(+ (* a a) (* b b))))
the algorithm seems to work, just turn
*y
to
* y
whitespace is important here, else you're telling the interpreter you want to usethe function *y
add a close paren after
(define (<= x y) (not (> x y))
sqrt-sum(a b)
turns to
(sqrt-sum a b)
and ditto for the other sqrt-sum calls
edit: also a possibility:
(define (square a) (* a a))
(define (square-sum a b c)
(- (+ (square a)
(square b)
(square c))
(square (min a b c))))