我以前总以为list comprehension这个语法糖不过就是些map,filter转换罢了,最近看到Haskell和Erlang的递归用法来实现排列,比循环方法要简洁很多:
Haskell:
java 代码
- permutation [] = [[]]
- permutation xs = [x:ys | x <- xs, ys <- permutation (delete x xs)]
Erlang:
java 代码
- permutation([]) -> [[]];
- permutation(L) -> [[H|T] || H <- L, T <- permutation(L--[H])].
应用举例:
AlbertLee出的一道面试题: http://www.douban.com/group/topic/1237059/
用1、2、2、3、4、5这六个数字(注意有两个2), 打印出所有不同的排列,如:512234、412325等,要求:"4"不能在第三位,"3"与"5"不能相连。
我稍微改了一下的Haskell解法如下:
java 代码
- module Main where
- import List
-
- inlist [] [] = True
- inlist a [] = False
- inlist a [x] = False
- inlist a (x:nx) = (a == [x, head nx] || a == [head nx, x]) || inlist a nx
- notinlist a b = not (inlist a b) --不相连判断
-
- permutation [] = [[]]
- permutation xs = [x:ys | x <- xs, ys <- permutation (delete x xs)] --排列
-
- third list = list!!2 /= 4 --第三位判断,这个硬编码啦
-
- s = [1,2,2,3,4,5]
- res2 = filter (notinlist [3,5]) $ filter third $ nub $ permutation s
- main = print (length res2)
更新:
Erlang在语法上和Haskell有不少类似如list comprehension,pattern match和immutable data,语意上则要简单很多,没有太多新概念比如lazy evaluation:
java 代码
- fibs = 0 : 1 : [ a + b | (a, b) <- zip fibs (tail fibs)]
这个使用递归的list comprehension来计算fibonacci数的方法Erlang是没法照搬的,因为fibs是个无限list。
更新2:
新版F#终于也增加了list comprehension,这样在F#可以写成:
java 代码
- let delete item list = List.filter (fun x -> x <> item) list
-
- let rec permutation x = match x with
- |[] -> [[]]
- |xs -> [for x in xs for y in permutation (delete x xs) -> x::ys]
分享到:
相关推荐
目前中文资料中讲解列表推导式最清晰的文档
什么是list,set,dict comprehension? 这其实是python中提供的遍历、筛选一个序列(如list,set,dict)中元素的特殊语法,...现在我们想得到另一个list,这个list中只含有strings中长度为4的字符串(‘list’和‘dic
lambda 函数 Python 支持一种有趣的语法,它允许你快速定义单行的最小函数。这些叫做 lambda 的函数,是从 Lisp 借用来的,可以用在任何需要函数的地方。 def f(x): return x*2,用lambda函数来替换可以写成:g = ...
'Neural Reading Comprehension and Beyond' 毕业论文上传斯坦福大学图书馆仅四天就获得了上千次的阅读量,成为了斯坦福大学近十年来最热门的毕业论文之一,她的这篇毕业论文主要研究神经网络阅读理解和问答,...
Mechanical Comprehension - ASVAB Section Three Mechanical Comprehension
Paragraph comprehension - ASVAB Section Eight Paragraph comprehension认证题库
Python-16~20题 Python-16~20题 ---16--- ---16--- Question: Question: >Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma- >Use a list comprehension...
Intermediate Listening Comprehension.xdf
个人学习记录——MRC_03
Comprehension orale 2 B1.pdf
接下来,我们将转向多语言和跨语言的MRC研究,介绍我们的一系列工作,讨论这些技术如何提高中文和其他语言的MRC系统性能。最后,我们将尝试以多语言的方式分析MRC模型,并以新的基准和方法在可解释的MRC中展示我们的...
Part III Listening Comprehension.doc
for…[if]…构建List (List comprehension) 1.简单的for…[if]…语句 Python中,for…[if]…语句一种简洁的构建List的方法,从for给定的List中选择出满足if条件的元素组成新的List,其中if是可以省略的。下面举几...
python基础教程:Python中在for循环中嵌套使⽤if和else语句的技巧 Python的语法糖⾮常强⼤,⽐如Python中在for循环中嵌套使⽤if和else语句的技巧便...但是,newList和a是不同的List。执⾏b=a,b和newList是不同的。newL
python循环语句嵌套_python基础教程:Python中在for循环中 嵌套使⽤if和。。。 Python的语法糖⾮常强⼤,⽐如Python中在for循环中嵌套使⽤if和else语句...但是,newList和a是不同的List。执⾏b=a,b和newList是不同的。
5-Comprehension and Knowledge.rar
Secondary School Admission Test - Reading Comprehension题库,SSAT-RC
本文实例分析了python开发之list操作。分享给大家供大家参考,具体如下: 对python中list的操作,... 3.Using a list comprehension:[x for x in iterable] 4.Using the type constructor:list() or list(iterable) '
listening comprehension专四改革后新题型PPT学习教案.pptx
Test of Essential Academic Skills - Reading Comprehension认证考试题库,TEAS-RC