lisp

Lambda Calculus CONS Pair implementation with Lisp

 ̄綄美尐妖づ 提交于 2019-12-24 14:41:17
问题 I'm trying to implement a Church Pair Lambda Calc. style with CLisp. According with Wikipedia: pair ≡ λx.λy.λz.z x y So far, this is my code: (defvar PAIR #'(lambda(x) #'(lambda(y) #'(lambda(z) (funcall (funcall z x) y)))) ) These are my FIRST and SECOND Functions: (defvar FIRST #'(lambda(p) (funcall(p TRUE))) ) (defvar SECOND #'(lambda(p) (funcall(p FALSE))) ) This 2 functions convert from Int to ChurchNumber (defun church2int(numchurch) (funcall (funcall numchurch #'(lambda (x) (+ x 1))) 0)

How to call native c function with windows HANDLE in common lisp / cffi

杀马特。学长 韩版系。学妹 提交于 2019-12-24 14:07:24
问题 native c header: typedef HANDLE HCAMERA; int Begin(HCAMERA* h); int End(HCAMERA h); HANDLE is defined: typedef void *HANDLE; native c source I want: HCAMERA h; int r = 0; r = Begin(&h); VERIFY(r); r = End(h); VERIFY(r); I tried following code in sbcl 1.3.1 but not working. (cffi:use-foreign-library "camera.dll") (cffi:defcfun "Begin" :int (handle :pointer)) (cffi:defcfun "End" :int (handle :pointer)) (defparameter *camera* (cffi:foreign-alloc :pointer)) ; alloc handle (cffi:with-foreign

Inorder traversal in lisp

五迷三道 提交于 2019-12-24 11:54:02
问题 I am trying to iterate through a tree inorder in Lisp. So far, I managed building the Postorder traversal but inorder gives me headaches.. The tree's format is this: A / \ B C (A 2 B 0 C 2 D 0 E 0) / \ D E (defun traverseTreeMain (l) (traverseTree l nil) ) (defun traverseTree (l lc) (cond ((and (null l) (null lc)) nil) ((null l) (append nil (traverseTree lc nil))) ((=(cadr l) 0) (append (list (car l)) (traverseTree (cddr l) lc) )) ((= (cadr l) 1) (append nil (traverseTree (cddr l) (append (

Return a pair - syntax error

。_饼干妹妹 提交于 2019-12-24 10:47:33
问题 I'm using pl in racket: https://pl.barzilay.org/ The download can be found here: http://pl.barzilay.org/pl.plt ( : f1 : -> (Pairof Symbol String)) (define (f1) (cons 'wwww "aaa")) Error: Type Checker: Polymorphic function `cons' could not be applied to arguments: Argument 1: Expected: a Given: 'wwww Argument 2: Expected: (Listof a) Given: String Result type: (Listof a) Expected result: (Pairof Symbol String) in: (cons (quote wwww) "aaa") What I did wrong and how can I fix it? 回答1: The #lang

for loop in scheme

我只是一个虾纸丫 提交于 2019-12-24 08:34:26
问题 i'm kinda of confused how i can construct a for loop in scheme. the for-loop should be implemented in Part II. where it takes a list of numbers and insert each element inside the list in Part I to find the length. I was cable to get the first element but i need a for loop or what so ever to get an output like this: '(7 10 5 16 106 37) here is my code : #lang racket ; Part I (define (sequence n) (cond [(= n 1) (list n)] [(even? n) ( cons n(sequence( / n 2)))] [(odd? n) ( cons n(sequence (+(* n

Scheme streams and circular lists

﹥>﹥吖頭↗ 提交于 2019-12-24 08:03:58
问题 In Scheme/Lisp I am trying to make a function that converts a list into a circular list. Therefore, I believe I need to construct an infinite stream in which the tail of the list points to the head of the list. Here is my code so far: (define (rotate-list l1 l1copy) (if (null? (force (cdr l1))) (cons (car l1) (delay l1copy))) (cons (car l1) (delay (rotate-list (force (cdr l1)) l1copy)))) All help is greatly appreciated. 回答1: No, you don't need streams to make a circular list. There are two

list should be a lambda expression

↘锁芯ラ 提交于 2019-12-24 06:38:45
问题 I've just started learning LISP and I'm just getting my head around the logic for it, however I've run into an error I can't find the solution for..I'm sure it's because I've misused a parentheses somewhere or I've misused a function in general, but I've been staring at it for an hour now and haven't made any progress! (defun not-touching (pos player move) (let (legal? t) if ((not (eq (member move '(0 1 2 3 4 7 8 11 12 13 14 15)) nil)) (mapcar #'(lambda(x) (if (not (member move x) nil) (cond

Occurrence of symbol A found anywhere in L. LISP

梦想与她 提交于 2019-12-24 06:10:15
问题 Here is my function (defun freq (symbol_A List_L) (cond ((atom (car List_L)) (cond ((eq (car List_L) symbol_A) t (+ 1 (freq symbol_A (cdr List_L)))) (t 0))) (T (freq symbol_A (cdr List_L)))) ) I am getting an error variable ATOM has no value. Here is what I am testing with (freq 'c '((a c) c e)) --> 2 (freq 'f '(((s) o ) d)) --> 0 (freq 'f '(((f) f) f f)) --> 4 Can not understand where is my error. I also tried this: (defun freq (a L) (cond ((null L) 0) ((equal a (car L)) (+ 1 (freq a (cdr L)

How to convert a flat list into a nested tree-like structure?

六眼飞鱼酱① 提交于 2019-12-24 05:12:09
问题 How to convert a flat list into an arbitrarily complex tree-like structure? First, a simple example, convert '(1 2 3 4) into '(1 (2 (3 (4)))) . I know how to do it with classical recursion: (defun nestify (xs) (if (null xs) (list) (list (car xs) (nestify (cdr xs))))) Now, what if the nested structure is arbitrarily complex? For example, I want to convert '(1 2 3 4 5 6 7 8) into '(1 (2 3) (4 (5 6) 7) 8) . How can I write a general function that is able to convert a flat list in any such nested

How to convert a flat list into a nested tree-like structure?

≯℡__Kan透↙ 提交于 2019-12-24 05:12:05
问题 How to convert a flat list into an arbitrarily complex tree-like structure? First, a simple example, convert '(1 2 3 4) into '(1 (2 (3 (4)))) . I know how to do it with classical recursion: (defun nestify (xs) (if (null xs) (list) (list (car xs) (nestify (cdr xs))))) Now, what if the nested structure is arbitrarily complex? For example, I want to convert '(1 2 3 4 5 6 7 8) into '(1 (2 3) (4 (5 6) 7) 8) . How can I write a general function that is able to convert a flat list in any such nested