scheme

Static signature for higher order function in Bigloo Scheme

吃可爱长大的小学妹 提交于 2019-12-11 09:50:25
问题 Does anyone know how to create a static signature for a higher order function in the module export section in the Bigloo Scheme language? Here is how far I got (module test (export (adder ::double))) (define (adder x) (lambda (y) (set! x (+ x y)) x)) The following will work, but I want to keep all the type data in the module declaration (module test (export (adder ::double))) (define (adder x) (lambda (y::double)::double (set! x (+ x y)) x)) 来源: https://stackoverflow.com/questions/12100256

Flatten once procedure

穿精又带淫゛_ 提交于 2019-12-11 09:49:03
问题 I'm having a bit of a struggle with coding a procedure that flattens a list once, i.e (flatten-once '((b) (c f) ((d)(e)))) would produce '(b c f (d) (e))) . I checked up on a few sources on how the standard flatten procedure works but it implements functions that are not included in the intermediate student with lambda language form I'm required to use. As far as I have it figured out a foldr would be somewhat helpful and have managed to get this (define (flatten-once lst) (cond [(empty? lst)

get pairs out of a huffman tree

冷暖自知 提交于 2019-12-11 09:44:29
问题 I'm trying to write a procedure Huffman-leaves; the procedure returns a list of pairs from a created huffman tree. Example on how it runs (huffman-leaves sample-tree) ->((A . 8) (C . 5) (B . 1) (D . 1)) What I've comed up with but got writers block... (define (huffman-leaves tree) (define (huffman-get-pairs current-branch pairs) (if (or (null? tree) (null? current-branch)) pairs (let ((next-branch (get-branch (car current-branch) current-branch))) (not (member? next-branch pairs) (if (leaf?

Local state of a variable

帅比萌擦擦* 提交于 2019-12-11 08:28:08
问题 I am trying to fully understand Objects and local states of their variables This code seems to produce different results for the same procedure called multiple times, meaning the local variable changes: (define new-withdraw (let ((balance 100)) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))) For this other code, it produces the same result, which means it creates a new local variable for every procedure call: (define (make

removing an item from a list in scheme

寵の児 提交于 2019-12-11 08:23:42
问题 How would you update a list by removing the first item? consider the following pseudocode define remover (list = cdr list)) or is there a more recommendable way of removing the first item in a list? 回答1: You nailed it, just write it as a procedure: (define (remover lst) (cdr l)) And use it like this (this creates a new binding, and is not an assignment): (let ((new-list (remover old-list))) new-list) Or like this (this defines a new list): (define new-list (remover old-list)) In any case, be

Scheme quicksort - Exception: attempt to apply non-procedure (1 2 3 4 5 7 …)

雨燕双飞 提交于 2019-12-11 08:17:44
问题 I just started learning Scheme (Petite Chez Scheme) and as a challenge to myself I'm trying to implement quicksort. However I get the following exception when I run it: Exception: attempt to apply non-procedure (1 2 3 4 5 7 ...) Here's my Scheme session from Emacs: Petite Chez Scheme Version 8.4 Copyright (c) 1985-2011 Cadence Research Systems > (define (set a i k) (define (reduce-list a i n) (if(= i n) a (reduce-list (cdr a) (+ i 1) n))) (if(= i 0) (cons k (cdr a)) (let ((b (cons k (reduce

How to implement iteration of lambda calculus using scheme lisp?

我的未来我决定 提交于 2019-12-11 08:04:38
问题 I'm trying to learn lambda calculus and Scheme Lisp. The tutorial on lambda calculus can be found here http://www.inf.fu-berlin.de/lehre/WS03/alpi/lambda.pdf. The problem I'm facing is I don't know how to properly implement iteration. (define (Y y) (((lambda (x) (y (x x))) (lambda (x) (y (x x)))))) (define (sum r n) ((is_zero n) n0 (n succ (r (pred n))))) (display ((Y sum) n5)) I always get this error: Aborting!: maximum recursion depth exceeded I know the problem is about the evaluation

Unable to get implementation of Y combinator working

你说的曾经没有我的故事 提交于 2019-12-11 07:56:53
问题 Here's the code (also here): #lang racket (define poorY ((lambda length (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))]))) (lambda length (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))]))))) When I run it: > (poorY '(9 7 8)) . . application: not a procedure; expected a procedure that can be applied to arguments given: '(#<procedure>) arguments...: '(#<procedure>) The screenshot looks like this: I'm using DrRacket as the repl. What's wrong

Eliminate inner parenthesis runs into empty list and doesn't eliminate using cons

血红的双手。 提交于 2019-12-11 07:54:02
问题 The goal is to eliminate all inner parenthesis. (flatten '(a (b c) d)) becomes '(a b c d) This is my code in Racket ; if slist is null, return empty ; otherwise, if it is a pair, recursively solve car and cdr and concat them ; if it is a symbol, return the symbol (define flatten (lambda (slist) (cond [ (null? slist) '()] [ (pair? slist) (cons ((flatten (car slist)) (flatten (cdr slist))))] [ (symbol? slist) slist]))) It's complaining procedure application: expected procedure, given: c;

Check if the item in list or sub-list

老子叫甜甜 提交于 2019-12-11 07:49:02
问题 I want to use the check function to check if the item is in the list or the sub-list. But the error really confuse me. Can someone tell me what's wrong with my code? (define check (lambda(item lis) (cond((null? lis) #f) (else(if(pair? (car lis)) (if(check item (car lis)) #t (check item (cdr lis))) (if(equal? item (car list)) #t (check item (cdr lis)))))))) > (check 'a '(a b)) . . car: contract violation expected: pair? given: #<procedure:list> 回答1: You have a typo in here: (equal? item (car