I know my answer is dead wrong, I would like to know how I would be able to reach the answer. What did I do wrong?
X = (setf X '(88 ((11 21 31 41)) (90 91 92 93)))
I have to write LISP expression which evaluates to the list:
(11 (11 21 31 41) (88 90 91 92 93))
My Answer:
(list (caadr X) (cdddr X))
These should get you going:
CL-USER> (cadr X)
((11 21 31 41))
CL-USER> (caadr X)
(11 21 31 41)
CL-USER> (caaadr X)
11
CL-USER> (cons (caaadr X) (cadr X))
(11 (11 21 31 41))
CL-USER> (caddr X)
(90 91 92 93)
CL-USER> (car X)
88
CL-USER> (cons (car X) (caddr X))
(88 90 91 92 93)
(Experimenting in the REPL is a very effective way of finding things out.)
Related
I am trying to create a simple script that imports old records to a new PostgreSQL database using the same Schema as the old one.
My initial idea was to read the contents of the .csv and create a map data structure. After that, insert it to the newly created database, using something like Honey SQL. Hopefully that makes sense.
I might be doing something wrong, or I probably am overthinking something here.
Let's say I have a script with the following functions:
(defn csv-data->map
"Takes the data from the csv file and
and stores it in a map structure."
[csv-data]
(map zipmap
(->> (first csv-data)
(map keyword)
repeat)
(rest csv-data)))
;; the map structure
(def big-map (with-open [reader (io/reader "my-file.csv")]
(doall
(csv-data->map (csv/read-csv reader)))))
So, I want to get the information stored in this map structure and import it in a PostgreSQL database.
Is there a way I can do that?
Also, here are the dependencies I am using, which are mostly from the official documentation for each lib.
(:require [clojure.data.csv :as csv]
[clojure.java.io :as io]
[next.jdbc :as jdbc]
[honey.sql :as sql]
[honey.sql.helpers :refer [select select-distinct from
join left-join right-join
where for group-by having union
order-by limit offset values columns
update insert-into set composite
delete delete-from truncate] :as h]
[clojure.core :as c])
It'll be great to see your critique and proposals!
My honey code is fairly simple:
(-> (h/insert-into :account)
(h/values [big-map])
(sql/format {:pretty true}))
The error message I get when trying to evaluate in the REPL is this
Unhandled clojure.lang.ExceptionInfo
These SQL clauses are unknown or have nil values: :ID,
:BILLINGLONGITUDE... etc
and under it, I get the real objects like so:
{:ID "000123456000000",
:BILLINGLONGITUDE "",... etc
Adding a little update on the problem:
So I am connected to the PG database and I can perform a simple query to get some count from a table account, but I get the following problem:
Unhandled org.postgresql.util.PSQLException ERROR: relation "account" does not exist Position: 13
QueryExecutorImpl.java: 2477 org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse
QueryExecutorImpl.java: 2190 org.postgresql.core.v3.QueryExecutorImpl/processResults
QueryExecutorImpl.java: 300 org.postgresql.core.v3.QueryExecutorImpl/execute
PgStatement.java: 428 org.postgresql.jdbc.PgStatement/executeInternal
PgStatement.java: 354 org.postgresql.jdbc.PgStatement/execute PgPreparedStatement.java:
169 org.postgresql.jdbc.PgPreparedStatement/executeWithFlags
PgPreparedStatement.java: 158
org.postgresql.jdbc.PgPreparedStatement/execute
result_set.clj: 669 next.jdbc.result-set/stmt->result-set
result_set.clj: 664 next.jdbc.result-set/stmt->result-set
result_set.clj: 944 next.jdbc.result-set/eval9989/fn
protocols.clj: 34 next.jdbc.protocols/eval9066/fn/G
result_set.clj: 1023 next.jdbc.result-set/eval10028/fn
protocols.clj: 34 next.jdbc.protocols/eval9066/fn/G
jdbc.clj: 250 next.jdbc/execute!
jdbc.clj: 237 next.jdbc/execute!
REPL: 43 csv-lein.core/eval11779
REPL: 43 csv-lein.core/eval11779
Compiler.java: 7194 clojure.lang.Compiler/eval
Compiler.java: 7149 clojure.lang.Compiler/eval
core.clj: 3215 clojure.core/eval
core.clj: 3211 clojure.core/eval
interruptible_eval.clj: 87 nrepl.middleware.interruptible-eval/evaluate/fn/fn
AFn.java: 152 clojure.lang.AFn/applyToHelper
AFn.java: 144 clojure.lang.AFn/applyTo
core.clj: 667 clojure.core/apply
core.clj: 1990 clojure.core/with-bindings*
core.clj: 1990 clojure.core/with-bindings*
RestFn.java: 425 clojure.lang.RestFn/invoke
interruptible_eval.clj: 87 nrepl.middleware.interruptible-eval/evaluate/fn
main.clj: 437 clojure.main/repl/read-eval-print/fn
main.clj: 437 clojure.main/repl/read-eval-print
main.clj: 458 clojure.main/repl/fn
main.clj: 458 clojure.main/repl
main.clj: 368 clojure.main/repl
RestFn.java: 1523 clojure.lang.RestFn/invoke
interruptible_eval.clj: 84 nrepl.middleware.interruptible-eval/evaluate
interruptible_eval.clj: 56 nrepl.middleware.interruptible-eval/evaluate
interruptible_eval.clj: 152 nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
AFn.java: 22 clojure.lang.AFn/run
session.clj: 218 nrepl.middleware.session/session-exec/main-loop/fn
session.clj: 217 nrepl.middleware.session/session-exec/main-loop
AFn.java: 22 clojure.lang.AFn/run
Thread.java: 833 java.lang.Thread/run
I'm writing a program which packs hexadecimal strings into bytes and writes them on disk. I expect the hexdump of the file to be same as the hexadecimal things. I'm doing this in Clojure:
(defn- hex-char->int
[hex-char]
(-> hex-char
str
(Integer/parseInt 16)))
(defn- pack
[hex-1 hex-2]
(-> hex-1
(bit-shift-left 4)
(bit-or hex-2)
unchecked-char))
(defn- hex-str->packed-bytes
[hex-str]
(->> hex-str
(map hex-char->int)
(partition 2)
(mapv (partial apply pack))))
(defn write-bytes
[bs]
(with-open [f (io/output-stream "test.txt")]
(.write f (.getBytes bs))))
(defn test-write
[hex-str]
(->> hex-str
hex-str->packed-bytes
(apply str)
write-bytes))
This program works well for hex couples from "00" to "7f". I can see the same hex numbers when I hexdump the output file.
But for characters from "80" to "ff", this doesn't work. The hexdump for "80" is "c280" and for "ff" it is "c3bf".
This gets solved if I don't convert to characters and directly write with bytes, so I assume that this is related to encoding.
I even found this: https://superuser.com/questions/1349494/filling-file-with-0xff-gives-c3bf-in-osx
But I want to understand how to solve this in Clojure's context.
Pasting the hexdump of `000f101f202f303f404f505f606f707f808f909fa0afb0bfc0cfd0dfe0eff0ff" for reference:
00000000 00 0f 10 1f 20 2f 30 3f 40 4f 50 5f 60 6f 70 7f |.... /0?#OP_`op.|
00000010 c2 80 c2 8f c2 90 c2 9f c2 a0 c2 af c2 b0 c2 bf |................|
00000020 c3 80 c3 8f c3 90 c3 9f c3 a0 c3 af c3 b0 c3 bf |................|
00000030
Please help me solve this.
Thanks! :)
As you suspected, the problem is in encoding. I'm guessing that the problem is happening when you (apply str) in test-write. So, I slightly re-wrote your code as follows:
user> (defn- hex-char->int
[hex-char]
(-> hex-char
str
(Integer/parseInt 16)))
#'user/hex-char->int
user> (defn- pack
[hex-1 hex-2]
(-> hex-1
(bit-shift-left 4)
(bit-or hex-2)))
#'user/pack
user> (defn- hex-str->packed-bytes
[hex-str]
(->> hex-str
(map hex-char->int)
(partition 2)
(mapv (partial apply pack))))
#'user/hex-str->packed-bytes
user> (defn write-bytes
[bs]
(with-open [f (io/output-stream "test.txt")]
(.write f bs)))
#'user/write-bytes
user> (defn test-write
[hex-str]
(->> hex-str
hex-str->packed-bytes
(mapv unchecked-byte)
(byte-array)
write-bytes))
#'user/test-write
user> (test-write "000f101f202f303f404f505f606f707f808f909fa0afb0bfc0cfd0dfe0eff0ff")
nil
user>
And showing the contents of the resultant file in hex:
dorabs-imac:example dorab$ od -h test.txt
0000000 0f00 1f10 2f20 3f30 4f40 5f50 6f60 7f70
0000020 8f80 9f90 afa0 bfb0 cfc0 dfd0 efe0 fff0
0000040
I have this function to create a list with initial element from the other question list with initial-element are start from 99 to 0 in Lisp
(defun newList (&optional(n 100))
(loop for i from (- n 1) downto 0 collect i))
(defun board (newList &optional(n 10))
(cond
((null newList) nil)
(t (cons (subseq newList 0 n) (board (subseq newList n) n)))))
(defun show-board (board)
(format T "~%")
(mapcar (lambda (x) (format T " ~A ~%" x)) board)
(format nil "")
)
(show-board (board (newList)))
(99 98 97 96 95 94 93 92 91 90)
(89 88 87 86 85 84 83 82 81 80)
(79 78 77 76 75 74 73 72 71 70)
(69 68 67 66 65 64 63 62 61 60)
(59 58 57 56 55 54 53 52 51 50)
(49 48 47 46 45 44 43 42 41 40)
(39 38 37 36 35 34 33 32 31 30)
(29 28 27 26 25 24 23 22 21 20)
(19 18 17 16 15 14 13 12 11 10)
(9 8 7 6 5 4 3 2 1 0)
see the result here https://ideone.com/Paorct
and with this function to remove duplicate number
(defun remove-duplicate (pred l)
(cond ((null l) NIL)
((funcall pred (car l)) (remove-duplicate pred (cdr l)))
(T (cons (car l) (remove-duplicate pred (cdr l))))))
I wanna implement a function that receives a the list and will randomly change its numbers. Make a recursive function and use the
nth
function, the random function, and the
remove-duplicate
function where the function must remove the number from the list equal to the one found randomly.
The stop condition is for the list to be empty;
should use the
let
statement to locally store the number found at a random position using the following statement:
(nth (random (length l)) l)
Using the
remove-duplicate
function you should remove from the list that is being passed as an argument in the recursive function, the number that was found randomly and that is stored locally.
I have this but it´s not work and I tried to understand the algorithm
my doubt is here, how to implement the function to shuffle list
without duplicate number
(defun shuffle-list (l)
;; iterate 99 times
(dotimes (i (- (length l) 1))
;; store random number to n
(let ((n (nth (random (length l)) l)))
;; print value of n
(format t "~A ~%" n)
(cond
((null l) nil)
;; I have this but it´s not show the new list
(t (remove-duplicate #'(lambda (x) (= x n)) l))))))
the result for example should be
(94 25 54 89 21 8 36 14 41 96)
(78 47 56 23 5 49 13 12 26 60)
(0 27 17 83 34 93 74 52 45 80)
(69 9 77 95 55 39 91 73 57 30)
(24 15 22 86 1 11 68 79 76 72)
(81 48 32 2 64 16 50 37 29 71)
(99 51 6 18 53 28 7 63 10 88)
(59 42 46 85 90 75 87 43 20 31)
(3 61 58 44 65 82 19 4 35 62)
(33 70 84 40 66 38 92 67 98 97)
There is a built-in function remove-duplicates whose name ends with an "s".
remove-duplicates takes a :from-end keyword argument, with a boolean value, to control which of the duplicate values is kept: the first or the last that occurs in a list.
Please read in Practical Common Lisp about let and about if/cond. It's a great book!
That remove-duplicate definition takes a predicate and removes elements for which the predicate is true. It behaves the same as the builtin remove-if.
So you have to create a predicate function that compares a list item to the n saved in (let ((n (nth (random (length list)) list))). Learn about variables, lambda, and closures.
How does the BLC encode parenthesis? For example, how would this:
λa.λb.λc.(a ((b c) d))
Be encoded in BLC?
Note: the Wikipedia article is not very helpful as it uses an unfamiliar notation and provides only one simple example, which doesn't involve parenthesis, and a very complex example, which is hard to analyze. The paper is similar in that aspect.
If you mean the binary encoding based on De Bruijn indices discussed in the Wikipedia, that's actually quite simple. You first need to do De Bruijn encoding, which means replacing the variables with natural numbers denoting the number of λ binders between the variable and its λ binder. In this notation,
λa.λb.λc.(a ((b c) d))
becomes
λλλ 3 ((2 1) d)
where d is some natural number >=4. Since it is unbound in the expression, we can't really tell which number it should be.
Then the encoding itself, defined recursively as
enc(λM) = 00 + enc(M)
enc(MN) = 01 + enc(M) + enc(N)
enc(i) = 1*i + 0
where + denotes string concatenation and * means repetition. Systematically applying this, we get
enc(λλλ 3 ((2 1) d))
= 00 + enc(λλ 3 ((2 1) d))
= 00 + 00 + enc(λ 3 ((2 1) d))
= 00 + 00 + 00 + enc(3 ((2 1) d))
= 00 + 00 + 00 + 01 + enc(3) + enc((2 1) d)
= 00 + 00 + 00 + 01 + enc(3) + 01 + enc(2 1) + enc(d)
= 00 + 00 + 00 + 01 + enc(3) + 01 + 01 + enc(2) + enc(1) + enc(d)
= 000000011110010111010 + enc(d)
and as you can see, the open parentheses are encoded as 01 while the close parens are not needed in this encoding.
My .emacs file has the following content:
$ cat ~/.emacs
(setq vc-handled-backends nil)
(global-linum-mode t)
$ od -xcb ~/.emacs
0000000 7328 7465 2071 6376 682d 6e61 6c64 6465
( s e t q v c - h a n d l e d
050 163 145 164 161 040 166 143 055 150 141 156 144 154 145 144
0000020 622d 6361 656b 646e 2073 696e 296c 280a
- b a c k e n d s n i l ) \n (
055 142 141 143 153 145 156 144 163 040 156 151 154 051 012 050
0000040 6c67 626f 6c61 6c2d 6e69 6d75 6d2d 646f
g l o b a l - l i n u m - m o d
147 154 157 142 141 154 055 154 151 156 165 155 055 155 157 144
0000060 2065 2974
e t )
145 040 164 051
0000064
These are absolutely valid Emacs's LISP expressions.
But recently whenever I start emacs, the line numbers no longer show up, instead, an error comes up:
$emacs --debug-init ~/.emacs
Debugger entered--Lisp error: (void-function global-linum-mode)
(global-linum-mode t)
eval-buffer(#<buffer *load*> nil "/Users/user/.emacs" nil t) ; Reading at buffer position 53
load-with-code-conversion("/Users/user/.emacs" "/Users/user/.emacs" t t)
load("~/.emacs" t t)
#[nil "^H\205\276^# \306=\203^Q^#\307^H\310Q\202A^# \311=\2033^#\312\307\313\314#\203#^#\315\202A^#\312\307\313\316$
command-line()
normal-top-level()
Version of emacs:
$ emacs --version
GNU Emacs 22.1.1
Copyright (C) 2007 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
Does anyone have an idea what might have caused this?
Thanks
Looks like linum mode was added to the Emacs distribution in version 23.1 (changelog). Because linum isn't distributed with Emacs 22, you're calling an undefined function and therefore getting an error.
Perhaps you used to run a more recent version of emacs, which has since been clobbered. You could either:
download the linum source, add it to your load path, then require it
install a newer version of Emacs.
Edit: As mentioned in the comments above, you could have multiple Emacs binaries on your path, with different versions. Have a look in /usr/bin, /opt/local/bin, et al, to see if this is the case.
First thing I would check the contents of that file with something other than cat such as the octal dump program od if you're running under a UNIXy system:
od -xcb .emacs
Because 53 characters is about the size of those two lines, it might have some rubbish at the end of (or within) the file.