I have a list of strings in kumo. I am printing three strings. I am getting them on 3 lines. I want them on one line separated by spaces. I am using the following code:
(display (first kumo))
(display (fourth kumo))
(display (second kumo))
or
(printf "~a~a~a" (first kumo)(fourth kumo)(second kumo))
Hmm... unless I'm misunderstanding you, Racket already does that. Here's a small (complete) program illustrating this:
#lang racket
(define kumo (list "the" "very" "big" "dog"))
(printf "~a~a~a" (first kumo)(fourth kumo)(second kumo))
... which produces
thedogvery
If you want spaces between the words, put them in the format string:
#lang racket
(define kumo (list "the" "very" "big" "dog"))
(printf "~a ~a ~a" (first kumo)(fourth kumo)(second kumo))
... which produces
the dog very
You can do the same thing with display, by displaying a string containing a single space in between the calls that display the words.
If I had to guess at your problem, I'd say that the strings you're displaying have newlines embedded in them.
Related
Currently, I have a function item counts that is meant to count the number of elements in a string and order them into pairs if it occurs once or more.
(check-expect
(item-counts
(cons "hello"
(cons "hey"
(cons "hello" '())))
equal?)
(cons "hello" 2
(cons "hey" 1) '()))
(check-expect
(item-counts
(cons #true
(cons #false
(cons #true '())))
equal?)
(cons #true 2
(cons #false 1 '())))
(define (item-counts lop ef)
(cond
[(empty? (rest lop))
(first lop)]
[(cons? (rest lop))
(if
(ef (first lop) (item-counts (rest lop) ef))) ... ???
As shown by the tests, I wish to add a number to the existing list if one of the elements of this list occurs more than once. I used the equal? function to identify the number of times an element occurs, but I am lost on how to add the number to the existing list as well as condense the repeating elements when producing a new list. I have tried using the append function but it creates a list in a different format ( (list (list) ).
I don't know if this is self-learning, for a class, etc. I know there are standard HtDP ways to do this, recipes, etc. But I don't know these things, sorry. What I will say is that Racket is quite easy to prototype things. You can just describe your problem and don't think too much about the details. I usually like to start in the middle.
For example:
At some point in the middle of execution you will have strings yet to examine and an association list of the strings examined so far. Then your task is 1) if there are no more strings to get, we're done, otherwise 2) get the next string, add it to the table, and continue because we're not done.
(define (build-assoc from-string current-assoc)
(if (done? from-string)
current-assoc
(let ((first-string (get-first-string from-string)))
(let ((result-table (add-string first-string current-assoc))
(result-string (drop-first-string from-string)))
(build-assoc result-string
result-table)))))
Maybe you don't use this procedure in the end. But it's a picture of what you're doing, and it suggests what sort of helper procedures you will need to finish your quest. Give yourself a fighting chance and just use DrRacket to outline your problem. When you see it, then maybe it gives you some ideas how to answer the questions. DrRacket helpfully points out all your unanswered questions like "done?: unbound identifier in: done?" Ah yes, how do we know we're done, good question DrRacket. Well you see, I was given this list of strings, so...
I'm writing a program to test student homework submissions. The student programs use the teaching languages (Beginning Student, et al). My program is using a sandbox, macros, etc. and is thus written in #lang racket and will be run with racket, not DrRacket.
When I need to display a message to students that contains a value, I'd really like it to display using the same printer that Beginning Student uses. For example, messages should contain
true rather than #true or #t
(cons 1 (cons 2 (cons 3 empty))) rather than '(1 2 3)
empty rather than '()
Any suggestions how to do this?
I find it interesting that a trivial BSL program executed with Racket respects some parts of the metadata in the header but not others. For example, consider
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname test) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #t)))
(cons true (cons 1 (cons 2 empty)))
Running this in DrRacket produces
(cons true (cons 1 (cons 2 empty)))
but running it with racket test.rkt produces
(cons #true (cons 1 (cons 2 '())))
Switching test.rkt to using beginning student with list abbreviations produces (list true 1 2) in DrRacket but (list #true 1 2) using the command line.
Note that racket is respecting the settings for printing the list, but not for printing Booleans or empty.
I am trying to write a lisp interpreter using only lisp primitive functions. I was writing my eval but I am not sure how I can detect the ' so I don't evaluate the symbol. I understand that 'x is converted to (quote x) internally, I fired up a lisp interpreter and tried the following:
(defun my-car (x) (car x))
(my-car (quote x)) -> QUOTE
(my-car '(x)) -> QUOTE
(my-car 'x) -> Error: Attempt to take the car of A which is not listp.
I see that in the first two examples car detects the quote as being the first element and returns it, I am not sure why it doesn't do that in the last example though since essentially 'x should be converted to (quote x) then passed as an argument to my-car. I need to check for this for one of the base cases of my-eval so that if a quote precedes an atom I don't return its value. Is there a way to do that using only primitive functions?
Thanks!
Lisp evaluation happens in stages.
The first stage is the reader, which converts the text (sequence of characters) to forms, i. e. lists, symbols, and literal forms (characters, strings, numbers, arrays etc.).
The reader also converts ' to a wrapped quote form around the following form (which may be a list, a symbol, etc.). 'a is read as (quote a), '(a) is read as (quote (a)).
Eval then only needs a rule how to handle quote as an operator. It never sees any '.
Your lisp interpreter is not behaving like Common Lisp. You should get:
(defun my-car (x) (car x))
(my-car (quote x)) -> Error: Attempt to take the car of A which is not listp.
(my-car '(x)) -> X
(my-car 'x) -> Error: Attempt to take the car of A which is not listp.
(my-car (list 'QUOTE 'X)) -> QUOTE
(my-car ''x) -> QUOTE
(my-car (quote 'x)) -> QUOTE
(my-car '(quote x)) -> QUOTE
Step by step:
Source code:
(my-car ''x)
Parse:
(my-car (quote (quote x)))
Evaluate arguments
(#<Function MY-CAR> (quote x))
Call function:
X
This I because the car of the list of the symbol QUOTE and symbol X is QUOTE.
I am having a hard time reading/understanding the syntax of the pcase statement in emacs-lisp. Please help me figure out how to make the following a valid pcase statement.
(defun execute-play (str)
(setq parse (mapcar (lambda (s) (split-string s ":")) (split-string str " ")))
(pcase (string-to-char (caar parse))``
((pred (<= (string-to-char "5"))) (t-to-pparse))
((pred (<= (string-to-char "d"))) (f-to-p parse))
((string-to-char "w") (w-to-p parse))
(_ "bad input")))
Note that typical input is "1:2 3" or "a 5".
The error from emacs that I get is: 'edebug-signal: Unknown upattern '(string-to-char w)'
This is the second to last case, -- I thought that this would just match the value of (caar parse) against (string-to-char "w") if it did not already match a case before this. Note that I also tried replacing (string-to-char "w") with (SELFQUOTING (string-to-char "w")) since the documentation says that: SELFQUOTING matches itself. This includes keywords, numbers, and strings.
Please help me get this emacs-lisp pcase statement working -- Thanks for all the help!
There are multiple issues with your code:
Since you're not doing any binding or deconstruction in your patterns, you don't need pcase — the conditional is better written using cond.
You have a spurious pair of backquotes at the end of line 3.
You appear to have inverted the first two tests — the first clause will match if the expression is larger than ?5, so the remaining clauses will never match.
pcase doesn't seem to support matching against evaluated values, so third clause should be written (pred (equal (string-to-char "0"))).
Generally, I can use the excellent rx macro to create readable regular expressions and be sure that I've escaped the correct metacharacters.
(rx (any "A-Z")) ;; "[A-Z]"
However, I can't work out how to create shy groups, e.g. \(?:AB\). rx sometimes produces them in its output:
(rx (or "ab" "bc")) ;; "\\(?:ab\\|bc\\)"
but I want to explicitly add them. I can do:
(rx (regexp "\\(?:AB\\)"))
but this defeats the point of rx.
In a perfect world, I'd like to be able to write:
(rx (shy-group "A"))
I'd settle for something like this (none of these work):
;; sadly, `regexp` only accepts literal strings
(rx (regexp (format "\\(?:%s\\)" (rx WHATEVER))))
;; also unfortunate, `eval` quotes the string it's given
(rx (eval (format "\\(?:%s\\)" (rx WHATEVER))))
How can I create regular expressions with shy groups using rx?
I think the structure of a rx form eliminates any need to explicitly create shy groups -- everything that a shy group could be needed for is accounted for by other syntax.
e.g. your own example:
(rx (or "ab" "bc")) ;; "\\(?:ab\\|bc\\)"
For other cases, it is also possible to extend the keywords used by rx.
Example (taken from EmacsWiki link above):
(defmacro rx-extra (&rest body-forms)
(let ((add-ins (list
`(file . ,(rx (+ (or alnum digit "." "/" "-" "_"))))
`(ws0 . ,(rx (0+ (any " " "\t"))))
`(ws+ . ,(rx (+ (any " " "\t"))))
`(int . ,(rx (+ digit))))))
`(let ((rx-constituents (append ',add-ins rx-constituents nil)))
,#body-forms)))