乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 16|回复: 0

[编程交流] '(Recursion passing-funct

[复制链接]

11

主题

98

帖子

87

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
55
发表于 2022-7-6 09:30:30 | 显示全部楼层 |阅读模式
Hi folks,
 
Whilst playing around with recursive list manipulation I took a bit of time experimenting with some of the quirkier aspects of Lisp and thought some of them pave the way for an interesting discussion thread on the forum.
 
  1. Recursion;sample list(setq sample '((0 5 0) (10 12 16) (44 65 97)                            (((0 0 0) (10 20 20) (1 1 3)                                ((0 0 2)    (2 2 2)) (4 4 4)))))(defun c:foo( /  ) (bar  sample ))(defun bar(lst  / x)   (mapcar       '(lambda(x)            (if(listp x)                (bar x)             (someFunction x)       lst))
The bar method is pretty much a bog standard recursive function call itself when x is a lst and calling a function when it is not. Which isn't particularly interesting on it's own .
 
  1. Passing function lists;Recursively evaluate list for multiples of three, return t or nil in place of original ;value.(defun c:foo( / f  ) (setq f '(if(>(rem x 3) 0) nil t)) (bar f sample ))(defun bar(func lst  / x)   (mapcar       '(lambda(x)            (if(listp x)              (bar func x)              (eval func)))       lst))
The new parameter func in the bar method is an unevaluated list. The idea with this is that you can pass a partial function to be evaluated on individual items. For me this is interesting because f's definition can be doesn't have to be a static method  For example.
  1. (defun c:foo( / f) (if(equal  (strcase (getstring "\nReturn remainder?(y/n)")) "Y")   (setq f  '(rem x 3))   (setq f '(if(>(rem x 3) 0) nil t)) )(bar f sample))
Which again is where the beauty of it is. Say you wanted to add handling for strings  you could always extend the definitions of x to include a stringp arguement.
 
Finally on the recusion part. Say you have a script that creates a list that is in effect an object or structure definition; the listp is only checks that items are a list. Which could then be handled by extending the definition of bar again to encompass a validation method pass.
  1. (bar '(...) 'isCustomObject sample)(defun bar(func valid lst  / x)   (mapcar       '(lambda(x)            (if(valid x)              (bar func x)              (eval func)))       lst))(defun isCustomObject( item /) ;;Some validation method)
As an end the below code is a fun quirk of the eval.
  1. (setq f1 '(if(= func f1) (setq func f2)(setq func f1)))(setq f2 '(if(= func f2) (setq func f1)(setq func f2)))(setq f f1)(bar f 'listp sample)
This completely useless partial function (unless you like to gamble on the end function i suppose) just swaps the function list that will be called. The quirk of this is that it changes the value of func from inside the eval statement which is evaluating func.
 
Anyway I'm not condoning the use of any of this but I as commented above I thought my endevours might be of interest to the forum.
 
Also it would be good to see any other quirks that forum users have come across.
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-7 01:50 , Processed in 0.377981 second(s), 65 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表